The runScript
method is used to execute Google Apps Scripts in the context of the Helix Client Apps project. Here’s a detailed explanation of the method, including how to specify script files, pass input parameters, and handle the returned result.
Specifying Script Files
To specify the script file to run, use the scriptPath
parameter. This parameter takes a string value that represents the path to the script file in the project’s file system. For example:
runScript('path/to/myScript.gs');
Passing Input Parameters
The runScript
method also allows you to pass input parameters to the script. You can pass parameters as an object with key-value pairs, where the keys correspond to the parameter names in the script, and the values are the actual parameter values. For example:
const inputParams = {
param1: 'value1',
param2: 'value2'
};
runScript('path/to/myScript.gs', inputParams);
In the script file, you can access these parameters using the params
object. For example:
function myScript(e) {
const param1 = e.parameter.param1;
const param2 = e.parameter.param2;
// rest of the script
}
Handling the Returned Result
The runScript
method returns a Promise that resolves to the result of the script execution. You can handle this result using then
and catch
methods. For example:
runScript('path/to/myScript.gs', inputParams)
.then(result => {
console.log('Script execution result:', result);
})
.catch(error => {
console.error('Error executing script:', error);
});
In the script file, you can return a value using the return
statement. For example:
function myScript(e) {
const result = 'This is the result';
return result;
}
Additional Options
The runScript
method also accepts the following optional parameters:
targetPlatform
: A string value that specifies the target platform for the script execution. This parameter is useful when running scripts that interact with specific G Suite apps.timeout
: A string value that specifies the timeout for the script execution. If the script does not complete within the specified time, the execution is terminated.user
: A string value that specifies the user to run the script as. This parameter is useful when running scripts that require specific permissions.
For more information, refer to the API Reference for Python and the Google Apps Script documentation.
Examples
Here are some examples of using the runScript
method with different options:
- Running a standalone script:
runScript('path/to/myScript.gs');
- Running a container-bound script:
runScript('path/to/myScript.gs', {}, 'container');
- Running a script with input parameters:
const inputParams = {
param1: 'value1',
param2: 'value2'
};
runScript('path/to/myScript.gs', inputParams);
- Running a script with a timeout:
runScript('path/to/myScript.gs', {}, undefined, undefined, '1m');
- Running a script as a specific user:
runScript('path/to/myScript.gs', {}, undefined, '[email protected]');
- Running a script with all options:
const inputParams = {
param1: 'value1',
param2: 'value2'
};
runScript('path/to/myScript.gs', inputParams, 'container', 'v8', '1m', '[email protected]');