[英]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)简单触发器取消选中该复选框。
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:要更新您的脚本时区:
Project Settings setting
Project Settings setting
Show "appsscript.json" manifest file in editor
checkbox. Show "appsscript.json" manifest file in editor
复选框。appsscript.json
file to "timeZone": "Asia/Tokyo"
appsscript.json
文件中的 timeZone 属性更改为"timeZone": "Asia/Tokyo"
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();
}
}
}
e.source
and e.range
respectively.e.source
和e.range
获取电子表格object 和范围object。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。
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()删除空单元格值。
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)以这种格式转换日期值
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()取消选中该复选框
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.