我有以下script。
我有一个包含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
}
嗨Srik,
非常感谢现在正在努力。
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
}
答案 0 :(得分:0)
当您的函数作为触发器运行时,没有活动工作表。仅当用户打开电子表格时,活动工作表才适用。修改您的脚本以使用getSheetByName
,一切都应该正常。
如果您希望在多个工作表上使用它,只需从另一个函数调用此函数..
function shellFunction(){
var sheets = ['sheet1','sheet2',etc];
for (var s in sheets){
toTrigger(sheets[s]);
}
}
并更改您的toTrigger
函数以接收工作表名称作为参数。
function toTrigger(sheetName){
var sh = SpreadsheetApp.openById(ID OF SPREADSHEET).getSheetByName(sheetName);
/* Your regular code */
}