简体   繁体   English

使用谷歌应用脚本在谷歌工作表中自动勾选复选框

[英]Automatic ticking checkbox in google sheet using google app script

enter image description here在此处输入图像描述

I have a problem as follows.我有一个问题如下。 First: on the holidays in column A respectively, I have colored the Order list table.首先:分别在 A 列的假期中,我为订单列表表着色。

Second:In line F on holidays (in column A), no ticking in the check box i tried it but not success this is my code第二:在假期的F行(在A列),复选框中没有勾选我试过但没有成功这是我的代码

function checkbox() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet1');
  if (sheet.getRange('C2').getValue() =='6/2/2021'||sheet.getRange('C2').getValue() 
     =='7/2/2021') {
                     sheet.getRange('C9').setValue('FALSE');
}   else {
            sheet.getRange('C9').setValue('FALSE');
         }
}

You can use onEdit(e) simple trigger to uncheck the checkbox if the date is a holiday.如果日期是假期,您可以使用onEdit(e)简单触发器取消选中该复选框。


Pre-requisite:先决条件:

  • Make sure your Google Sheet Timezone and your Script Timezone is the same.确保您的 Google 表格时区和您的脚本时区相同。

Upon checking your shared document, your Google Sheet Timezone is set to Tokyo(+09:00) but your Script timezone is set to America/Los_Angeles .检查您的共享文档后,您的 Google 表格时区设置为 Tokyo(+09:00) 但您的脚本时区设置为America/Los_Angeles

To update your Script timezone:要更新您的脚本时区:

  1. At the left, click Project Settings setting在左侧,点击Project Settings setting
  2. Select the Show "appsscript.json" manifest file in editor checkbox. Select Show "appsscript.json" manifest file in editor复选框。
  3. Change the timeZone property in your appsscript.json file to "timeZone": "Asia/Tokyo"将您的appsscript.json文件中的 timeZone 属性更改为"timeZone": "Asia/Tokyo"

Sample Code:示例代码:

function onEdit(e){

  var ss = e.source
  var sheetName = '注文記入表';
  var sheet = ss.getActiveSheet();
  var cell = e.range;

  Logger.log(cell.isChecked());
  //Check if modified cell is a checkbox and if it in sheet 注文記入表
  if(sheet.getName()==sheetName && cell.isChecked() != null){

    //Get Holidays
    var holidays = sheet.getRange('A1:A').getDisplayValues().flat().filter(String);

    //Get date value
    var dateVal = sheet.getRange(4,cell.getColumn()).getValue();
    Logger.log(dateVal);
    var dateStr = Utilities.formatDate(new Date(dateVal),Session.getScriptTimeZone(),"dd/MM/yyyy");
    Logger.log(dateStr);

    //If current date is a holiday, uncheck the checkbox
    Logger.log(holidays.includes(dateStr));
    if(holidays.includes(dateStr)){
      cell.uncheck();
    }
  }

}

What it does?它能做什么?

  1. Get the spreadsheet object and range object using the event objects e.source and e.range respectively.分别使用事件对象e.sourcee.range获取电子表格object 和范围object。
  2. Check if the modified cell is in sheet '注文記入表' and if it is a checkbox using range.isChecked()使用range.isChecked()检查修改后的单元格是否在工作表“注文记入表”中,是否为复选框

range.isChecked() range.isChecked()

Returns whether all cells in the range have their checkbox state as 'checked'.返回范围内的所有单元格是否将其复选框 state 设置为“已选中”。 Returns null if some cells are checked and the rest unchecked, or if some cells do not have checkbox data validation.如果选中了某些单元格并且未选中 rest,或者如果某些单元格没有复选框数据验证,则返回 null。

  1. If the modified cell is a checkbox, get the list of holidays in column A. range.getDisplayValues() returns a 2-d array of string values, use Array.prototype.flat() to change it to 1-d array of string values.如果修改后的单元格是一个复选框,则在 A 列中获取假期列表。 range.getDisplayValues()返回一个二维字符串值数组,使用Array.prototype.flat()将其更改为一维字符串数组价值观。 Then use Array.prototype.filter() to remove empty cell values.然后使用Array.prototype.filter()删除空单元格值。

  2. Get the date of the checkbox using range.getValue() .使用range.getValue()获取复选框的日期。 Since your holiday list of dates is in DD/MM/YYYY format, convert the date value in this format using Utilities.formatDate(date, timeZone, format)由于您的假期日期列表采用DD/MM/YYYY格式,因此请使用Utilities.formatDate(date, timeZone, format)以这种格式转换日期值

  3. Check if converted date in Step 4 is listed in your holiday array list using Array.prototype.includes() .使用Array.prototype.includes()检查步骤 4 中的转换日期是否列在您的假期数组列表中。 If it is found in the holiday array list, un-check the checkbox using range.uncheck()如果在假期数组列表中找到它,请使用range.uncheck()取消选中该复选框


Note:笔记:

  • You cannot debug/run this onEdit() in the script editor manually.您无法在脚本编辑器中手动调试/运行此 onEdit()。 If you want to test this, you need to include logs in your code, edit a cell in your sheet and check the logs in the executions tab.如果您想对此进行测试,您需要在代码中包含日志,编辑工作表中的单元格并检查执行选项卡中的日志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM