13

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();
}
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Satinder singh
  • 10,100
  • 16
  • 60
  • 102
  • Have you tried using script properties instead of user properties? https://developers.google.com/apps-script/reference/properties/properties-service#getScriptProperties() – Gerardo Oct 27 '15 at 23:13
  • yes I tried both `getScriptProperties()` and `getUserProperties()` and both not working in library.js – Satinder singh Oct 28 '15 at 05:08

1 Answers1

9

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();
}
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
  • Thanks for the reply, so basically each script has its own property, but we can send parameter and fetch them and set as PropertiesService. it's like each library has own PropertiesService and we have set same values to them – Satinder singh Nov 06 '15 at 05:43
  • Thanks for the reply. It also helped me a lot. I just wanted to suggest an improvement in the code above. You might use <> instead of looping over the parameters. Here's the link to the function's documentation : https://developers.google.com/apps-script/reference/properties/properties#setProperties(Object) – iceberg53 Nov 29 '22 at 11:56