Home > front end >  Error with data for each row function problem with brackets and colons
Error with data for each row function problem with brackets and colons

Time:07-28

Hello I am new to the community and I am a novice coder with very little coding experience. I understand some basics and 1st part of the code is working. I am having a problem with the data.foreach(funtion(row) where it is giving a error with brackets and colons

function myFunction() {
  var Name = 1;
  var Surname = 2;
  var AffilliateID = 24;

 var emailTemp = HtmlService.createHtmlOutputFromFile("Affiliate email");
} 
var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Affiliate Responses");
var data = ws.getDataRange("A4:Y"   ws.getLastRow()).getDisplayValues();  

data.forEach(function(row) **(** 
emailTemp.Name = row[Name]**;**
emailTemp.Surname = row[Surname];
emailTemp.AffilliateID = row[AffiliateID]; 

))

I have created a var for each line and the tutorial I am following expresses the code as is above. The tutorial may be outdated and some help with an explanation would be appreciated. The bold is the errors.

Thanks

Glenn

CodePudding user response:

For your loop it's something like that

data.forEach(row => {
  emailTemp.Name = row[Name]
  emailTemp.Surname = row[Surname];
  emailTemp.AffilliateID = row[AffiliateID]; 
})

But to use your

 var Name = 1;
 var Surname = 2;
 var AffilliateID = 24;
 var emailTemp = HtmlService.createHtmlOutputFromFile("Affiliate email");

You have to be in the same scope

CodePudding user response:

you need to declare correctly the function that you are passing as a parameter to forEach

const data = [2,5,1,3,4]
data.forEach(function myFunction(item){
    console.log(item)
})

you can also use arrow functions:

const data = [2,5,1,3,4]
data.forEach(item => console.log(item))

CodePudding user response:

Welcome to the community. This seems like a simple syntax issue.

You're using ( & ) brackets for function braces, when infact they should be { & } (like your first function).

It's also important that your variables in the right scope. You cannot access the emailTemp variable as it is scoped to your myFunction function. I've moved this into the global scope for you.

Your updated code would look something like this:

function myFunction() {
  var Name = 1;
  var Surname = 2;
  var AffilliateID = 24;
} 

var emailTemp = HtmlService.createHtmlOutputFromFile("Affiliate email");
var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Affiliate Responses");
var data = ws.getDataRange("A4:Y"   ws.getLastRow()).getDisplayValues();  

data.forEach(function(row) { 
  emailTemp.Name = row[Name];
  emailTemp.Surname = row[Surname];
  emailTemp.AffilliateID = row[AffiliateID];
});
  • Related