如何将参数传递给库脚本中的定时触发函数

Sat*_*ngh 10 google-sheets google-apps-script google-spreadsheet-api

我创建了一个在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();
}
Run Code Online (Sandbox Code Playgroud)

在我的SpreadSheet中

Code.gs:

function startOnce(){
    Library1.carHistory("US-xxx","Honda");
}

function startEvery30mins(){
    Library1.startEvery30mins_CarHistory("US-xxx","Honda");
}
Run Code Online (Sandbox Code Playgroud)

编辑:

Code.gs:我尝试使用PropertiesService,但仍然无法正常工作

function startOnce(){
    var uProps = PropertiesService.getUserProperties();
    uProps.setProperty('Maker', 'Honda');
    uProps.setProperty('Number', 'US-xxx');

    Library1.carHistory();
}
Run Code Online (Sandbox Code Playgroud)

图书馆 :

 function carHistory()
    {
        // Fetch Parametr
        var getProps=PropertiesService.getUserProperties();
        var c_Maker= getProps.getProperty('Maker');
        var c_Number=getProps.getProperty('Number');
       // code logic

    }
Run Code Online (Sandbox Code Playgroud)
function startEvery30mins_CarHistory()
{
      ScriptApp.newTrigger("carHistory")
      .timeBased()
      .everyMinutes(30)
      .create();
}
Run Code Online (Sandbox Code Playgroud)

Mog*_*dad 6

属性是非共享资源; 每个脚本都有自己的一组属性,这些属性不与其他脚本共享.此行为会影响您如何处理库中的属性,如资源范围中所述.

非共享资源意味着库和包含脚本都只具有对其资源实例的内置访问权限.但是,库可以通过对其进行操作的显式函数来提供对其非共享资源的访问.

换一种说法; 该库的未共享属性可以被暴露到主脚本通过库函数.

此功能可用于设置库触发功能的操作参数:

/**
 * 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);
  }
}
Run Code Online (Sandbox Code Playgroud)

你会这样使用它:

function startOnce(){
  var uProps = {
    'Maker':'Honda',
    'Number':'US-xxx'
  });

  Library1.setParameters(uProps);
  Library1.carHistory();
}
Run Code Online (Sandbox Code Playgroud)