Aloe Francisco
Aloe Francisco

Reputation: 11

Issue using forEach on google script

Hi I have a syntax error on a GoogleScript. this is my first script ever so I'm not really sure why is not working. The idea is to pick up information from a google sheet and create a label from a doc template and storage it in a folder

function readSheet() {

const labelFolder = DriveApp.getFolderById("1NTnJgjCewcFcrtMsl5DtRgrvFUNCdsvt"); 

const docFile = DriveApp.getFileById("1pRy9BLDuDka7-9PKnHGgO1AtgpoWIY45t7ypVhXXM8I");

const sheet = SpreadsheetApp.gettActiveSpreadSheet().getSheetByName("test");

const data = sheet.getRange(2,1,sheet.getLastRow()-1,10).getValues();

  data.forEach(row => {

      shippinglabels(row [5],row [6],row [8],docFile,labelFolder);
});

}

function shippinglabels(user,address,package,docFile,labelFolder) {
   
  
  const labelFile = docFile.makeCopy(labelFolder);

  const tempDocFile = DocumentApp.openById(labelFile.getId());

  const body = tempDocFile.getBody();

  body.replaceText("{user}", user);

  body.replaceText("{address}", address);

  body.replaceText("{package}", package);

  tempDocFile.setName(user);

  tempDocFile.saveAndClose(); 
  

     
    }

Thanks

Upvotes: 0

Views: 539

Answers (2)

Aloe Francisco
Aloe Francisco

Reputation: 11

I change the forEach form this:

data.forEach(row => {
    
          shippinglabels(row [5],row [6],row [8],docFile,labelFolder);
    });

To this:

    data.forEach(function(row) {
      shippinglabels(row [5],row [6],row [8],docFile,labelFolder);
});

and did the job.

Thanks for the help

Upvotes: 1

Tanaike
Tanaike

Reputation: 201378

Please modify as follows and test it again.

From:

const sheet = SpreadsheetApp.gettActiveSpreadSheet().getSheetByName("test");

To:

const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("test");
  • In this case, gettActiveSpreadSheet() is getActiveSpreadsheet().
  • I think that after above modification was reflected to your script, when the folder ID, Google Document ID and the values from Google Spreadsheet are correct, no error occurs. When other error occurred, can you provide the detail information about it? I would like to confirm it.

Reference:

Upvotes: 1

Related Questions