我有以下脚本。
情况:
我有一个包含 10 个工作表和 15 个用户登录并修改它的电子表格。
脚本功能:
在特定列的活动表中插入两天之间的天数,脚本比较来自列 J 和 TodayDay 的日期,并在列 F 中插入差异。
问题:
我无法使用定时触发器运行此脚本。我想为并发工作表每 15 分钟运行一次此脚本。
测试用例:
如果我在电子表格中只有一张工作表,触发器只会随时间运行。如果我有不止一张纸,触发器不会随时间运行。如果我将相同的触发器设置为 OnEdit,则触发器只会运行多张纸。
我需要每 15 分钟为所有工作表或活动工作表运行此脚本,因为我还有另外两个运行 OnEdit 的脚本。当我也在 OnEdit 中设置此脚本时,电子表格会慢慢转动。
function onOpen() {
var menuEntries = [ {name: "UpdateAge", functionName: "toTrigger"},
];
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.addMenu("Tools",menuEntries);//
}
// create a timer trigger that will call "toTrigger" every 15 minutes
function toTrigger(){
var sh = SpreadsheetApp.getActiveSheet();
var data = sh.getRange(1,1,sh.getLastRow(),sh.getLastColumn()).getValues();
for(var n=0;n<data.length;++n){
if(typeof(data[n][9])=='object'){
data[n][5]=dayToToday(data[n][9])
}
}
sh.getRange(1,1,data.length,data[0].length).setValues(data)
}
function dayToToday(x){
var refcell = x;;// get value in column A to get the reference date
var refTime = new Date(refcell);
var ref = refTime.setHours(0,0,0,0)/(24*3600000);// set hours, minutes, seconds and milliseconds to 0 if necessary and get number of days
var today = new Date();
var TD = today.setHours(0,0,0,0)/(24*3600000);// set hours, minutes, seconds and milliseconds to 0 if necessary and get number of days
var day = parseInt(TD-ref);// get the difference in days (integer value )
return day ; // return result that will be in cell
}
编辑:工作
嗨,斯里克,
非常感谢现在正在工作。
function onOpen() {
var menuEntries = [ {name: "UpdateAge", functionName: "shellFunction"},
];
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.addMenu("Tools",menuEntries);//
}
function shellFunction(){
var sheets = ['Sheet1','Sheet2','Sheet3','Sheet4'];
for (var s in sheets){
toTrigger(sheets[s]);
}
}
// create a timer trigger that will call "toTrigger" every 15 minutes
function toTrigger(sheetName){
var ss = SpreadsheetApp.openById('SHEET ID');
var sh = ss.getSheetByName(sheetName);
var data = sh.getRange(1,1,sh.getLastRow(),sh.getLastColumn()).getValues();
for(var n=0;n<data.length;++n){
if(typeof(data[n][9])=='object'){
data[n][5]=dayToToday(data[n][9])
}
}
sh.getRange(1,1,data.length,data[0].length).setValues(data)
}
function dayToToday(x){
var refcell = x;;// get value in column A to get the reference date
var refTime = new Date(refcell);
var ref = refTime.setHours(0,0,0,0)/(24*3600000);// set hours, minutes, seconds and milliseconds to 0 if necessary and get number of days
var today = new Date();
var TD = today.setHours(0,0,0,0)/(24*3600000);// set hours, minutes, seconds and milliseconds to 0 if necessary and get number of days
var day = parseInt(TD-ref);// get the difference in days (integer value )
return day ; // return result that will be in cell
}