Home > database >  Run function for an array of values
Run function for an array of values

Time:06-01

I'm trying to run a couple of functions for an array of values and it's not working. Essentially what I want to do is paste and export a PDF of the range A1:C15 for every cell in Z. I have tried a couple of things and nothing seems to work, the latest code I tried is the following:

function GuardarPDF(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
  var refesSheet = ss.getSheetByName("etiqueta");
  var lista_refes = refesSheet.getRange("Z4:Z"   refesSheet.getLastRow()).getValues();
  var lista_refes_ok = lista_refes.filter(([a]) => a);
  
 for (var i = 0; i < lista_refes_ok.length; i  ) {
    console.log(lista_refes_ok[i][0]) // Here, you can see the folder ID in the log.
    var refes = lista_refes_ok[i][0];
  try {
    for (var i = 0; i < lista_refes_ok.length; i  ) {
    console.log(lista_refes_ok[i][0]) // Here, you can see the folder ID in the log.
    var refes = lista_refes_ok[i][0];
    try {
       var referencia2 = refesSheet.getRange("B5").setvalue(refes);
    CreaPDF();
    }}

(CreaPDF works fine, I'm having trouble generating the iteration so that a PDF for every row in Z is generated) Does anybody know where the problem is or how to solve it? Thank you so much in advance! Here is the spreadsheet

CodePudding user response:

Your code had a variety of errors, but I think this is what you are looking for:

Try:

function GuardarPDF() {

  const ss = SpreadsheetApp.getActiveSpreadsheet()
  const refesSheet = ss.getSheetByName(`etiqueta`)
  const lista_refes = refesSheet.getRange(`Z4:Z${refesSheet.getLastRow()}`)
                                .getValues()
                                .flat()
                                .filter(cell => cell != ``)
  
  lista_refes.forEach(ref => {
    refesSheet.getRange(`B5`).setValue(ref)
    CreaPDF()
    Utilities.sleep(5000)
  })

}

This will get all values listed in Z4:Z(lastRow), then remove any blank cells. For each of the values, it will write the ref to cell B5 (potential error), and run CreaPDF.

If you need any further explanation, have any questions, or need modification, please let me know!

Learn More:

CodePudding user response:

Try this:

function myfunk() {
  const ss = SpreadsheetApp.getActive()
  const rsh = ss.getSheetByName(`Your Sheet Name`)
  const vs = rsh.getRange(4,26,rsh.getLastRow() - 3).getValues().flat().filter(e => e);
  vs.forEach(e => {
    rsh.getRange(`B5`).setValue(e);
    SpreadsheetApp.flush();
    CreaPDF()
  });
}
  • Related