Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameter(s) to timed trigger function in library script

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();
}
like image 456
Satinder singh Avatar asked Oct 27 '15 05:10

Satinder singh


People also ask

How do I create a timer trigger in Azure function?

Create a timer triggered function In your function app, select Functions, and then select + Create. Select the Timer trigger template. Configure the new trigger with the settings as specified in the table below the image, and then select Create. Defines the name of your timer triggered function.

How do you use triggers in app scripts?

Under Run, select the name of function you want to trigger. Under Events, select either Time-driven or the Google App that the script is bound to (for example, From spreadsheet). Select and configure the type of trigger you want to create (for example, an Hour timer that runs Every hour or an On open trigger).

How do I pass the function key to Azure function?

Obtaining keys To view your keys, create new ones, or roll keys to new values, navigate to one of your HTTP-triggered functions in the Azure portal and select Function Keys. You can also manage host keys. Navigate to the function app in the Azure portal and select App keys.

Can an Azure function have two triggers?

There are no plans to support multiple triggers per Function. You will have to create a Function for each EventHub. If there is common code that may be shared between Functions, you may move them to a helper method that can be called from each Function.

How to pass a parameter to a function from a trigger?

You can't pass a parameter when a function is launched from a trigger. You have to store this information somewhere to allow script find it. For example with what you say I understand you have some data in a spreadsheet, you can put this information in the spreadsheet.

What is the timer object in a timer Trigger?

When a timer trigger function is invoked, a timer object is passed into the function. The following JSON is an example representation of the timer object. The isPastDue property is true when the current function invocation is later than scheduled.

How does the @timertrigger annotation work?

The @TimerTrigger annotation on the function defines the schedule using the same string format as CRON expressions. The following example shows a timer trigger binding in a function.json file and a JavaScript function that uses the binding. The function writes a log indicating whether this function invocation is due to a missed schedule occurrence.

What is a timer Trigger in Azure Functions?

A timer trigger lets you run a function on a schedule. This is reference information for Azure Functions developers. If you're new to Azure Functions, start with the following resources: Create your first function: C#, JavaScript, Java, or Python.


1 Answers

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();
}
like image 190
Mogsdad Avatar answered Oct 01 '22 06:10

Mogsdad