Placing checkboxes in Google Sheets using Apps Script

前端 未结 8 1034
遥遥无期
遥遥无期 2020-12-10 02:14

I know that checkbox is a relatively new feature in Google Sheets, so I\'m trying to find a way to automatically create checkboxes in cells.

So far, I haven\'t foun

8条回答
  •  无人及你
    2020-12-10 03:02

    The checkbox is the recently added Data Validation criterion. Interestingly enough, when I attempt to call the 'getDataValidation()' method on the range that contains checkboxes, the following error is thrown:

    var rule = range.getDataValidation();
    

    We're sorry, a server error occurred. Please wait a bit and try again.

    In the meantime, you can work around this by placing a single checkbox somewhere in your sheet and copying its Data Validation to the new range. For example, if "A1" is the cell containing the checkbox and the target range consists of a single column with 3 rows:

    var range = sheet.getRange("A1"); //checkbox template cell
    var targetRange = sheet.getRange(rowIdex, colIndex, numOfRows, numOfCols);
    range.copyTo(col, SpreadsheetApp.CopyPasteType.PASTE_DATA_VALIDATION);
    var values = [["true"], ["false"], ["false"]];
    targetRange.setValues(values);
    

提交回复
热议问题