Home > other >  Uncheck checkbox in google sheets in loop
Uncheck checkbox in google sheets in loop

Time:04-23

I am trying to get the corresponding checkbox to uncheck after the function runs.

It originally logs as True (because it's checked), then assign false to it, then it logs as false. However, the spreadsheets still shows True and the checkbox is still checked.

Thanks

function getData() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();

  // Get data range and values
  var rows = ss.getDataRange().getValues();
  var teeTimes = [];

  // Loop through each row of range
  rows.forEach(function (row) {
    // If checkbox is true
    if (row[7] === true && row[8] === false) {
      Logger.log(row[7]); // Prints True

      row[7] = false;
      var singleTime = {};
      singleTime.day = Utilities.formatDate(row[1], "GMT", 'MMM d');
      singleTime.time = Utilities.formatDate(row[2], "EST", 'h:mm');
      singleTime.player1 = row[3];
      singleTime.player2 = row[4];
      singleTime.player3 = row[5];
      singleTime.player4 = row[6];
      teeTimes.push(singleTime);

      Logger.log(row[7]); // Prints False
    }
  });
}

Picture of spreadsheet

CodePudding user response:

Try it this way

function getData() {
  var ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName('Sheet0')
  var vs = sh.getRange(2,1,sh.getLastRow() -1, sh.getLastColumn()).getValues();
  vs.forEach((r,i) => {
    if (r[7] == true && r[8] == false) {
      vs[i][7] = "FALSE";
      Logger.log(r[7]); // Prints False
    }
  });
  sh.getRange(2,1,vs.length,vs[0].length).setValues(vs);
}

I took out the stuff that had nothing to do with the question. I also added the sh variable so that I could setValues();

You could also do it this way:

function getData() {
  var ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName('Sheet0')
  var vs = sh.getRange(2,1,sh.getLastRow() -1, sh.getLastColumn()).getValues();
  vs.forEach((r,i) => {
    if (r[7] == true && r[8] == false) {
      sh.getRange(i 2,8).setValue("FALSE");
      //Logger.log(r[7]); // Prints False
    }
  });
}
  • Related