Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set column value via script in google sheets

I am trying to index my google sheet, based on an if condition. So, if the 3 column data is not empty, I want the first column of the sheet to get indexed. In the code below, the if statement is working fine, but I am unable to set the value of index in the first column.

var sheetFrom = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("production");
var data = sheetFrom.getDataRange().getValues();
var count = 0; 
var farmerCount = 0;
for(n=3;n<data.length;++n){ // iterate in the array, row by row
      if (data[n][2]!=""){
        farmerCount++; 
        data[n][0].setValue(farmerCount); 
        //sheetFrom.getRange(n, 0).setValue(farmerCount);
       }

Thanks in advance.

like image 627
Zeeshan Rang Avatar asked May 31 '26 10:05

Zeeshan Rang


1 Answers

Range indexing in Google Apps Script is 1 based so you want to use

sheetFrom.getRange(n, 1).setValue(farmerCount); 

Possibly n+1 depending on how many header rows you have.

like image 104
Robin Gertenbach Avatar answered Jun 04 '26 12:06

Robin Gertenbach



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!