我创建了一个每隔 30 分钟触发一次的函数,我想传递一些参数。我有一个返回 carHistory 的库,以及我从中调用库函数的电子表格。
Library1.gs
function carHistory(number,maker)
{
// code logic
}
function startEvery30mins_CarHistory(number,maker)
{
//This function works
carHistory(number,maker);
// how to trigger this with parameter.
ScriptApp.newTrigger("carHistory")
.timeBased()
.everyMinutes(30)
.create();
}
在我的电子表格中
代码.gs:
function startOnce(){
Library1.carHistory("US-xxx","Honda");
}
function startEvery30mins(){
Library1.startEvery30mins_CarHistory("US-xxx","Honda");
}
编辑:
代码.gs:我尝试使用 属性服务 ,但仍然无法正常工作
function startOnce(){
var uProps = PropertiesService.getUserProperties();
uProps.setProperty('Maker', 'Honda');
uProps.setProperty('Number', 'US-xxx');
Library1.carHistory();
}
图书馆:
function carHistory()
{
// Fetch Parametr
var getProps=PropertiesService.getUserProperties();
var c_Maker= getProps.getProperty('Maker');
var c_Number=getProps.getProperty('Number');
// code logic
}
function startEvery30mins_CarHistory()
{
ScriptApp.newTrigger("carHistory")
.timeBased()
.everyMinutes(30)
.create();
}
最佳答案
属性是非共享资源;每个脚本都有自己的一组不与其他脚本共享的属性。此行为会影响您如何处理库中的属性,如 Resource Scoping 中所述。 .
A not-shared resource means that both library and the including script have built-in access only to their instance of the resource. However, a library can provide access to its not-shared resources by having explicit functions that operate on them.
换句话说;库的非共享属性可以通过库函数暴露给主脚本。
此函数可用于为您的库触发函数设置操作参数:
/**
* Set name,value pairs of parameters for library function.
*
* @param {Object} parameters Object with named properties to be
* used as library function parameters.
*/
function setParameters( parameters ) {
var props = PropertiesService.getUserProperties();
for (var key in parameters) {
var value = parameters[key];
props.setProperty(key, value);
}
}
你会像这样使用它:
function startOnce(){
var uProps = {
'Maker':'Honda',
'Number':'US-xxx'
});
Library1.setParameters(uProps);
Library1.carHistory();
}
关于google-apps-script - 如何将参数)传递给库脚本中的定时器触发函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33360803/