Richard Livingston
Richard Livingston

Reputation: 3

How can I write to a cell in a specific row in Google App Scripts inside of a conditional loop?

I would like to create a PDF for each row in my sheet that has not already had a PDF created. Im doing this by assigning a binary to rows based on their creation status and then conditioning my loop based on that binary.

My issue is that I cannot figure out how to set the value of the printed column to 1 after the loop iterates on that row.

function createBulkPDFs() {
  const docFile = DriveApp.getFileById("1zGTNkzUr_ApaYSpqdu_PKcEuDrVm9r9kA_oyqfIRWeI");
  const TempFolder = DriveApp.getFolderById("1h8RHp890f0HGsc-dulwEP9Urrpnq7xcw");
  const pdfFolder = DriveApp.getFolderById("1boy1E2Ih3Cp3zMTak8nAUtmtc7e34Y1L");
  const currentSheet  = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("WorkOrders");

  const data = currentSheet.getRange(2,1,currentSheet.getLastRow()-1,11).getDisplayValues();
  
  data.forEach(row => {
    var printed = row[10];
    if (printed !== 1) {
      createPDF(row[2], row[3], row[0], row[4], row[1], row[5],row[7], row[6], row[8], docFile, TempFolder, pdfFolder);
      SpreadsheetApp.getActiveSheet().getRange(printed).setValue(1); // This is where my code fails. "Range not found"
    }
  });

}

Upvotes: 0

Views: 41

Answers (1)

Wicket
Wicket

Reputation: 38131

Replace

data.forEach(row => {

and

SpreadsheetApp.getActiveSheet().getRange(printed).setValue(1);

by

data.forEach((row, i) => {

and

SpreadsheetApp.getActiveSheet().getRange(i + 2, 11).setValue(1);

respectively.

Resources

Upvotes: 1

Related Questions