Google脚本在特定日期(一个以上的日期)运行 [英] Google script run at specific date (more than one date)
问题描述
我需要帮助来编写脚本.这是我想做的:脚本是时间触发器(每天),但是运行时,我想添加特定的日期(每个rang(单元格)一个日期).并且如果日期不在选项中,脚本将运行但不会记录.
I need help to write a script. Here's what I want to do: the script is time trigger (every day), but when it run, I want to add specific date (one date per rang(cells)). And if the date is not in the option, the script will run but do noting.
这是我已经在这里提供帮助的脚本:
Here is the script that I have already work with the help from here:
function AddProtectionToColumn() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var range = GetRange(ss);
var protectSs = range.protect().setDescription('Protect');
var me = Session.getEffectiveUser();
protectSs.addEditor(me);
protectSs.removeEditors(protectSs.getEditors());
if (protectSs.canDomainEdit()) {
protectSs.setDomainEdit(false);
}
}
function GetRange(ss){
var today = new Date().getDate();
// assuming you're only making protected ranges on the first sheet
var protections = ss.getSheets()[0].getProtections(SpreadsheetApp.ProtectionType.RANGE);
if (at date == ('october 01, 2019')){
return ss.getRange('A1:B5');
if (at date == ('october 02, 2019')){
return ss.getRange('C1:D5');
}
}
脚本的一部分(保护)运行良好.我在日期设置方面遇到了麻烦.我尝试了一下,但没有成功.您有解决方案吗? 另外,我在脚本编写方面没有太多经验,我的母语是法语. 谢谢!!
The part of the script (protect) work well. I have trouble with the date setting thing. I try something and it didn't work. Do you have a solution for that? Also, I don't have much experience with scripting and my first language is French. Thank you!!
推荐答案
答案:
您可以使用getDate()
方法获取当前日期的数字,并使用类似于当前代码的条件.
Answer:
You can use the getDate()
method to get the number of the current date and use a conditional similar to your current code.
function GetRange(ss){
var today = new Date().getDate();
var protections = ss.getSheets()[0].getProtections(SpreadsheetApp.ProtectionType.RANGE);
if (today == 1){
return ss.getRange("day-1-range");
}
else if (today == 2){
return ss.getRange("day-2-range");
}
else if (today == 3){
return ss.getRange("day-3-range");
}
// else block continues...
else if (today == 31){
return ss.getRange("day-31-range");
}
}
老实说,我真的不建议这样做,因为您将使用很大的if块,但是对于您的用例来说,它将起作用.
Honestly, I don't really recommend this as you're going to have a very large if block, but for your use case this will work.
getDate()
方法返回一个数字,该数字等于日期对象的日期的数字日期.
例如:
var date1 = new Date('01 October 2019').getDate(); // == 1
var date2 = new Date('21/12/1984').getDate(); // == 21
var date3 = new Date('Jan 14 2000').getDate(); // == 14
var date4 = new Date('29 February 2004').getDate(); // == 29
这篇关于Google脚本在特定日期(一个以上的日期)运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!