Skip to content

This is a Javascript library to use "google.script.run" with the synchronous process.

License

Notifications You must be signed in to change notification settings

tanaikech/syncGoogleScriptRun

Repository files navigation

syncGoogleScriptRun

Build Status MIT License

Overview

This is a Javascript library to use "google.script.run" with the synchronous process.

Description

When I create Web Apps, add-on using a side bar and dialog, there is the case that I want to use google.script.run with the synchronous process. As you know, google.script.run works with the asynchronous process. So in order to use it as the synchronous process, the script is required to be prepared. I also saw several issues for such situation at Stackoverflow and other sites. I thought that when the script for achieving this was prepared as a library, it might be useful for users. So I created this.

Install

<script src="syncGoogleScriptRun.min.js"></script>

Or, using jsdelivr cdn

<script src="https://cdn.jsdelivr.net/gh/tanaikech/syncGoogleScriptRun@master/syncGoogleScriptRun.min.js"></script>
  • Of course, you can use this by directly copying and paste the script of syncGoogleScriptRun.js to the script editor.

Method

Method Explanation
syncGoogleScriptRun(object) Run a function of Google Apps Script with the synchronous process.
  • object: There are 2 properties.
    • gasFunction: Function name of Google Apps Script side.
    • arguments: Arguments for the function.

Usage

Sample script

In this sample script, the result is returned every 1 second.

When you use the following sample script, please copy and paste the following scripts to the container-bound script of Spreadsheet. And run openDialog(). By this, a dialog is opened at Spreadsheet. When you clicked the button, you can see the result at the console.

HTML side: index.html

<input type="button" value="Run script" onClick="run()">

<script src="https://cdn.jsdelivr.net/gh/tanaikech/syncGoogleScriptRun@master/syncGoogleScriptRun.min.js"></script>

<script>
async function run() {
  for (let i = 0; i < 5; i++) {

    const resource = {
      gasFunction: "myFunction", // Function name of Google Apps Script side
      arguments: i // Arguments for the function
    };
    const res = await syncGoogleScriptRun(resource).catch(e => {throw new Error(e)});

    console.log(res);
  }
}
</script>

Google Apps Script side: Code.gs

function myFunction(e) {
  Utilities.sleep(1000);
  return e;
}

function openDialog() {
  var html = HtmlService.createHtmlOutputFromFile("index");
  SpreadsheetApp.getUi().showModalDialog(html, "sample");
}

Result

When the button of "Run script" is clicked, you can see the following result at the console.

0
1
2
3
4

Licence

MIT

Author

Tanaike

If you have any questions and commissions for me, feel free to tell me.

Update History

  • v1.0.0 (September 13, 2019)

    1. Initial release.

TOP

About

This is a Javascript library to use "google.script.run" with the synchronous process.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published