Home > Net >  How do I delete the cell content in front of cell that have not been checked?
How do I delete the cell content in front of cell that have not been checked?

Time:02-28

I have cell range. First column have content. Second column have check box. See first picture.first picture I try to clear content infront of unchecked boxes.

    function clearCon() {
var ss = SpreadsheetApp;
var sheet=ss.getActiveSpreadsheet().getSheetByName ("AddData");
var chkval = sheet.getRange("A29:B33").getValues();
Logger.log(chkVal);
Logger.log( chkval.length);
for(var i=0; i<=chkVal.length; i  ) { if (chkval[1][1] == false) { sheet.getRange (28, 1, 1 1,1).clearContent(); return;
Logger.log(1);
}
}
}

See this picture code it's not correctly work. How to do this?

CodePudding user response:

You are only clearing the row 28 with your current code because you of sheet.getRange(28, 1, …). So in your forward loop, you are not moving down the rows. To fix this issue, just do sheet.getRange(29 i,1).clearContent().

CodePudding user response:

Description

First your script and picture of script don't match, so I'm not sure which one you are currently using.

Second, your for loop should be i<chkval.length.

Third, there should not be a return in your for loop.

Fourth, your sheet.getRange(29 i, 1).clearContent()

And lastly, if there are no formulas in the range you specified I would use setValues() as shown below.

Script

function clearCon() {
  try {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheetByName ("AddData");
    var chkval = sheet.getRange("A29:B33").getValues();
    // test for true or false
    chkval.forEach( row => row[0] = row[1] ? row[0] : '' );
    // extract column A
    chkval = chkval.map( row => row.slice(0,1) );
    console.log(chkval);
    sheet.getRange(29,1,chkval.length,1).setValues(chkval);
  }
  catch(err) {
    console.log(err);
  }
}

References

  • Related