Coder Social home page Coder Social logo

tanaikech / syncgooglescriptrun Goto Github PK

View Code? Open in Web Editor NEW
11.0 3.0 1.0 5 KB

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

License: MIT License

JavaScript 100.00%
javascript-library js-library google-apps-script web-apps sidebar dialog synchronous

syncgooglescriptrun's Introduction

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

syncgooglescriptrun's People

Contributors

tanaikech avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

thegreatgaspy

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.