Placing checkboxes in Google Sheets using Apps Script

前端 未结 8 1037
遥遥无期
遥遥无期 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:12

    Short answer

    Add the checkbox from the Google Sheets UI, then use one of the copyTo methods of Class Range.

    Explanation

    The Google Apps Script Spreadsheet service doesn't include a methods for everything that could be done through the Google Sheets user interface. This is the case of the Insert > Checkbox which is a pretty new feature.

    Even the Record macro feature can't do this. The following was recorded one momento ago

    /** @OnlyCurrentDoc */
    
    function InsertCheckbox() {
      var spreadsheet = SpreadsheetApp.getActive();
      spreadsheet.getRange('A1').activate();
      /*
       * Added to show the missing Insert > Checkbox step
       */
      spreadsheet.getRange('B1').activate();
      spreadsheet.getRange('A1').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
    };
    

    NOTE: If you don't want to pass all the cell properties (borders, formulas, background, etc. instead of SpreadsheetApp.CopyPasteType.PASTE_NORMAL use SpreadsheetApp.CopyPasteType.PASTE_DATA_VALIDATION.

    Related Q on Stack Overflow

    • Google Sheets: Add a CheckBox with a script

提交回复
热议问题