Placing checkboxes in Google Sheets using Apps Script

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

    You want to create the checkbox in the cells of spreadsheet using the scripts. If my understanding is correct, how about this workaround? Unfortunately, the Class SpreadsheetApp has no methods for creating the checkbox yet. (When such methods are tried to be used, the error occurs.) So I would like to propose to create it using Sheets API.

    When I saw ConditionType of dataValidation, the document of BOOLEAN says

    The cell's value must be TRUE/FALSE or in the list of condition values. Supported by data validation. Renders as a cell checkbox. ...

    From this, I could understand how to create the checkbox using Sheets API. The following script is a sample script. This creates 6 checkboxes to "A1:C3". When you use this script, please enable Sheets API at Advanced Google Services and API console as follows.

    Enable Sheets API v4 at Advanced Google Services

    • On script editor
      • Resources -> Advanced Google Services
      • Turn on Google Sheets API v4

    Enable Sheets API v4 at API console

    • On script editor
      • Resources -> Cloud Platform project
      • View API console
      • At Getting started, click Enable APIs and get credentials like keys.
      • At left side, click Library.
      • At Search for APIs & services, input "sheets". And click Google Sheets API.
      • Click Enable button.
      • If API has already been enabled, please don't turn off.

    If now you are opening the script editor with the script for using Sheets API, you can enable Sheets API for the project by accessing this URL https://console.cloud.google.com/apis/library/sheets.googleapis.com/

    Sample script :

    In this sample script, the checkboxes are created to "A1:C3" of Sheet1. Please use this script as the container-bound script.

    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheetId = ss.getSheetByName("Sheet1").getSheetId();
    var resource = {"requests": [
      {
        "repeatCell": {
          "cell": {"dataValidation": {"condition": {"type": "BOOLEAN"}}},
          "range": {"sheetId": sheetId, "startRowIndex": 0, "endRowIndex": 3, "startColumnIndex": 0, "endColumnIndex": 3},
          "fields": "dataValidation"
        }
      },
      {
        "updateCells": {
          "rows": [
            {"values": [{"userEnteredValue": {"boolValue": true}}, {"userEnteredValue": {"boolValue": false}}, {"userEnteredValue": {"boolValue": false}}]},
            {"values": [{"userEnteredValue": {"boolValue": true}}, {"userEnteredValue": {"boolValue": true}}, {"userEnteredValue": {"boolValue": false}}]},
            {"values": [{"userEnteredValue": {"boolValue": true}}, {"userEnteredValue": {"boolValue": true}}, {"userEnteredValue": {"boolValue": true}}]}
          ],
          "start": {"rowIndex": 0, "columnIndex": 0, "sheetId": sheetId},
          "fields": "userEnteredValue"
        }
      }
    ]};
    Sheets.Spreadsheets.batchUpdate(resource, ss.getId());
    
    Flow :
    1. dataValidation is set using repeatCell.
    2. boolValue is set using updateCells.

    Result :

    Note :

    • This is a simple sample script. So please modify this for your environment.
    • When the methods of the Class SpreadsheetApp for creating the checkbox can be used, I think that the following sample script might be able to be used.
    Script for Class SpreadsheetApp

    At June 22, 2018, this script returns an error of the server error yet.

    var rule = SpreadsheetApp.newDataValidation().withCriteria(SpreadsheetApp.DataValidationCriteria.CHECKBOX, ["TRUE", "FALSE"]).build();
    SpreadsheetApp.getActiveSheet().getRange("A1").setDataValidation(rule);
    

    References :

    • ConditionType
    • Advanced Google Services
    • Sheets API v4

    If I misunderstand your question, I'm sorry.

提交回复
热议问题