|
| 1 | +# syncGoogleScriptRun |
| 2 | + |
| 3 | +[](https://travis-ci.org/tanaikech/syncGoogleScriptRun) |
| 4 | +[](LICENCE) |
| 5 | + |
| 6 | +<a name="top"></a> |
| 7 | + |
| 8 | +# Overview |
| 9 | + |
| 10 | +This is a Javascript library to use "google.script.run" with the synchronous process. |
| 11 | + |
| 12 | +# Description |
| 13 | + |
| 14 | +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](https://developers.google.com/apps-script/guides/html/reference/run). 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. |
| 15 | + |
| 16 | +# Install |
| 17 | + |
| 18 | +```html |
| 19 | +<script src="syncGoogleScriptRun.min.js"></script> |
| 20 | +``` |
| 21 | + |
| 22 | +Or, using jsdelivr cdn |
| 23 | + |
| 24 | +```html |
| 25 | +<script src="https://cdn.jsdelivr.net/gh/tanaikech/syncGoogleScriptRun@master/syncGoogleScriptRun.min.js"></script> |
| 26 | +``` |
| 27 | + |
| 28 | +- Of course, you can use this by directly copying and paste the script of [syncGoogleScriptRun.js](https://github.com/tanaikech/syncGoogleScriptRun/blob/master/syncGoogleScriptRun.js) to the script editor. |
| 29 | + |
| 30 | +<a name="method"></a> |
| 31 | + |
| 32 | +# Method |
| 33 | + |
| 34 | +| Method | Explanation | |
| 35 | +| :-------------------------- | :----------------------------------------------------------------- | |
| 36 | +| syncGoogleScriptRun(object) | Run a function of Google Apps Script with the synchronous process. | |
| 37 | + |
| 38 | +- `object`: There are 2 properties. |
| 39 | + - `gasFunction`: Function name. |
| 40 | + - `arguments`: Arguments for the function. |
| 41 | + |
| 42 | +<a name="usage"></a> |
| 43 | + |
| 44 | +# Usage |
| 45 | + |
| 46 | +## Sample script |
| 47 | + |
| 48 | +In this sample script, the result is returned every 1 second. |
| 49 | + |
| 50 | +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. |
| 51 | + |
| 52 | +#### HTML side: `index.html` |
| 53 | + |
| 54 | +```HTML |
| 55 | +<input type="button" value="Run script" onClick="run()"> |
| 56 | + |
| 57 | +<script src="https://cdn.jsdelivr.net/gh/tanaikech/syncGoogleScriptRun@master/syncGoogleScriptRun.min.js"></script> |
| 58 | + |
| 59 | +<script> |
| 60 | +async function run() { |
| 61 | + for (let i = 0; i < 5; i++) { |
| 62 | + const resource = { |
| 63 | + gasFunction: "myFunction", // Function name |
| 64 | + arguments: i // Arguments for the function |
| 65 | + }; |
| 66 | + const res = await syncGoogleScriptRun(resource).catch(e => {throw new Error(e)}); |
| 67 | + console.log(res); |
| 68 | + } |
| 69 | +} |
| 70 | +</script> |
| 71 | +``` |
| 72 | + |
| 73 | +#### Google Apps Script side: `Code.gs` |
| 74 | + |
| 75 | +```javascript |
| 76 | +function myFunction(e) { |
| 77 | + Utilities.sleep(1000); |
| 78 | + return e; |
| 79 | +} |
| 80 | + |
| 81 | +function openDialog() { |
| 82 | + var html = HtmlService.createTemplateFromFile("index").evaluate(); |
| 83 | + SpreadsheetApp.getUi().showModalDialog(html, "sample"); |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +--- |
| 88 | + |
| 89 | +<a name="licence"></a> |
| 90 | + |
| 91 | +# Licence |
| 92 | + |
| 93 | +[MIT](LICENCE) |
| 94 | + |
| 95 | +<a name="author"></a> |
| 96 | + |
| 97 | +# Author |
| 98 | + |
| 99 | +[Tanaike](https://tanaikech.github.io/about/) |
| 100 | + |
| 101 | +If you have any questions and commissions for me, feel free to tell me. |
| 102 | + |
| 103 | +<a name="updatehistory"></a> |
| 104 | + |
| 105 | +# Update History |
| 106 | + |
| 107 | +- v1.0.0 (September 13, 2019) |
| 108 | + |
| 109 | + 1. Initial release. |
| 110 | + |
| 111 | +[TOP](#top) |
0 commit comments