0

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");

Kal El
  • 53
  • 6
  • 1
    You should not set values one at a time with `.setValue()`. Instead, set all values in one go with `.setValues()`. See [Apps Script best practices](https://developers.google.com/apps-script/guides/support/best-practices#use_batch_operations). – doubleunary Jun 12 '22 at 06:51
  • Copy of [How can I set a value inside a foreach loop in Google Sheets App Script](https://stackoverflow.com/q/72583707/13045193). – doubleunary Jun 12 '22 at 06:52

1 Answers1

1

Use the index parameter, like this:

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

doubleunary
  • 13,842
  • 3
  • 18
  • 51