如何将参数传递给库脚本中的定时触发函数 [英] How to pass parameter(s) to timed trigger function in library script
问题描述
我创建了一个函数,每30分钟后触发一次,我想传递一些参数。我有一个库返回carHistory,并从我的电子表格中调用库函数。
Library1.gs
函数carHistory(数字,制造商)
{
//代码逻辑
}
函数startEvery30mins_CarHistory(数字,制造商)
{
//这个函数可以工作
carHistory(number,maker);
//如何用参数触发这个。
ScriptApp.newTrigger(carHistory)
.timeBased()
.everyMinutes(30)
.create();
$ / code>
在我的电子表格中
Code.gs:
函数startOnce(){
Library1.carHistory US-XXX, 本田);
function startEvery30mins(){
Library1.startEvery30mins_CarHistory(US-xxx,Honda);
code
$ b $ hr
$ b 编辑: b
Code.gs:我尝试使用 PropertiesService ,但仍无法正常工作
函数startOnce(){
var uProps = PropertiesService.getUserProperties();
uProps.setProperty('Maker','Honda');
uProps.setProperty('Number','US-xxx');
Library1.carHistory();
库:
function carHistory()
{
//获取参数
var getProps = PropertiesService.getUserProperties();
var c_Maker = getProps.getProperty('Maker');
var c_Number = getProps.getProperty('Number');
//代码逻辑
code $
函数startEvery30mins_CarHistory()
{
ScriptApp.newTrigger(carHistory)
.timeBased()
。 everyMinutes(30)
.create();
}
解决方案属性是一个非共享资源;每个脚本都有自己的一组属性,这些属性不与其他脚本共享。此行为会影响您如何处理库中的属性,如资源范围。
非共享资源意味着库和包含脚本都仅对其资源实例具有内置访问权限。但是,图书馆可以通过对其进行操作的显式函数来提供对非共享资源的访问。
换句话说, 库的非共享属性可以通过库函数暴露给主脚本。
这个函数可以是用于为您的库触发器函数设置操作参数:
$ b
/ **
*设置库函数的名称,参数值对。
*
* @param {Object} parameters具有命名属性的对象为
*,用作库函数参数。
* /
函数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();
}
I have created a function which trigger every after 30 mins, and I want to pass some parameter. I have one library which returns carHistory, and my spreadsheet from where I call library function.
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();
}
In my SpreadSheet
Code.gs :
function startOnce(){
Library1.carHistory("US-xxx","Honda");
}
function startEvery30mins(){
Library1.startEvery30mins_CarHistory("US-xxx","Honda");
}
EDITED:
Code.gs: I tried using PropertiesService, but still not working
function startOnce(){
var uProps = PropertiesService.getUserProperties();
uProps.setProperty('Maker', 'Honda');
uProps.setProperty('Number', 'US-xxx');
Library1.carHistory();
}
Library :
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();
}
Properties are a not-shared resource; each script has their own set of Properties which are not shared with other scripts. This behaviour affects how you can handle properties in a Library, as described in 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.
In other words; the library's not-shared properties can be exposed to the main script by library functions.
This function can be used to set up the operating parameters for your library trigger function:
/**
* 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);
}
}
You'd use it like this:
function startOnce(){
var uProps = {
'Maker':'Honda',
'Number':'US-xxx'
});
Library1.setParameters(uProps);
Library1.carHistory();
}
这篇关于如何将参数传递给库脚本中的定时触发函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!