Home > Blockchain >  How to make "setValue" work inside a foreach in Google Sheets App Script
How to make "setValue" work inside a foreach in Google Sheets App Script

Time:06-13

I want to set values for rows that meet the conditions that I have set via the following script:

function add(){
 var mainsname ='MIIST';
  var  ss = SpreadsheetApp.getActiveSpreadsheet();
  var mainss = ss.getSheetByName(mainsname);
  var adcnumber = ss.getRangeByName('AD').getColumn();
  var sacnumber = ss.getRangeByName('SA').getColumn();
  var dicnumber = ss.getRangeByName('DI').getColumn();
  var range = mainss.getDataRange();
  var data = range.getValues().slice(1);
  data.forEach((row)=>{

  if(row[sacnumber-1] !== "" && row[dicnumber-1] =="" ){mainss.getRange(row,dicnumber 1).setValue("test");}

  } )
}

The issue is that I can't get the row number because "row" give me the value, I need the row number I can use the "setValue":

mainss.getRange(row,dicnumber 1).setValue("test");

CodePudding user response:

Use the index parameter, like this:

  data.forEach((row, rowIndex) => {
    const rowNumber = rowIndex   1;
    // ...

  • Related