0

-can anyone help me check on this code because even the condition was met it still setting the value of all the data. The data should only be updated when the condition is met.

var ss = SpreadsheetApp.openById('id');

var sheet = ss.getSheetByName("VL Request");

var range = sheet.getDataRange();

var values = range.getValues();

var rid = [1];

// writes the form data to the spreadsheet

for (var i = 1; i <= values.length; i++) {

    var sel = sheet.getRange(i, 2).getValue();

    if (sel = ReqNum) {

        sheet.getRange(i, 14).setValue(pri);

        sheet.getRange(i, 16).setValue(stat);

        sheet.getRange(i, 18).setValue(rem);
    }
}
2
  • 2
    This is an assignment sel = ReqNum this would be a comparison sel == ReqNum Commented Sep 20, 2019 at 17:33
  • ReqNum is undefined. rid is unused. Commented Sep 20, 2019 at 17:38

1 Answer 1

1

Try this:

function unknown() {
  var ss=SpreadsheetApp.openById('id');
  var sheet=ss.getSheetByName("VL Request");
  var range=sheet.getRange(2,1,sheet.getLastRow()-1,18);
  var values=range.getValues();
  for(var i=0;i<values.length;i++) {
    if(values[i][1]==ReqNum) {//First time through is row 2 column 2
      sheet.getRange(i+2, 14).setValue(pri);//i+2 is the row, 14 is the column
      sheet.getRange(i+2, 16).setValue(stat);//the first time through is row 2, column16
      sheet.getRange(i+2, 18).setValue(rem);
    }
  }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Hi Cooper, I've already tried but still not work. However, i am trying to understand this line: "var range=sheet.getRange(2,1,sheet.getLastRow()-1,18);" can you please explain how it works?
And just to clarify, values[i][1] is values[rows][column] right?
No. values is a two dimensional array whose index starts at zero. Columns and Rows start at one. I started my range at row 2 so I just had to correct the getRange rows to i+2
I think I already know what's the issue. Maybe the value of ReqNum has spaces or something that would not be equal on the data. Is there a way to fix that? Because the value of var ReqNum is coming from a form
You can use trim() or parseInt()
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.