├── .travis.yml ├── LICENCE ├── README.md ├── appsscript.json ├── syncGoogleScriptRun.js └── syncGoogleScriptRun.min.js /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - stable 4 | script: 5 | - echo "test temporarily disabled" 6 | -------------------------------------------------------------------------------- /LICENCE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright (c) 2019 Kanshi TANAIKE 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 5 | 6 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 7 | 8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # syncGoogleScriptRun 2 | 3 | [![Build Status](https://travis-ci.org/tanaikech/syncGoogleScriptRun.svg?branch=master)](https://travis-ci.org/tanaikech/syncGoogleScriptRun) 4 | [![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](LICENCE) 5 | 6 | 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 | 20 | ``` 21 | 22 | Or, using jsdelivr cdn 23 | 24 | ```html 25 | 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 | 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 of Google Apps Script side. 40 | - `arguments`: Arguments for the function. 41 | 42 | 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 | 56 | 57 | 58 | 59 | 73 | ``` 74 | 75 | #### Google Apps Script side: `Code.gs` 76 | 77 | ```javascript 78 | function myFunction(e) { 79 | Utilities.sleep(1000); 80 | return e; 81 | } 82 | 83 | function openDialog() { 84 | var html = HtmlService.createHtmlOutputFromFile("index"); 85 | SpreadsheetApp.getUi().showModalDialog(html, "sample"); 86 | } 87 | ``` 88 | 89 | #### Result 90 | When the button of "Run script" is clicked, you can see the following result at the console. 91 | 92 | ``` 93 | 0 94 | 1 95 | 2 96 | 3 97 | 4 98 | ``` 99 | 100 | --- 101 | 102 | 103 | 104 | # Licence 105 | 106 | [MIT](LICENCE) 107 | 108 | 109 | 110 | # Author 111 | 112 | [Tanaike](https://tanaikech.github.io/about/) 113 | 114 | If you have any questions and commissions for me, feel free to tell me. 115 | 116 | 117 | 118 | # Update History 119 | 120 | - v1.0.0 (September 13, 2019) 121 | 122 | 1. Initial release. 123 | 124 | [TOP](#top) 125 | -------------------------------------------------------------------------------- /appsscript.json: -------------------------------------------------------------------------------- 1 | { 2 | "timeZone": "Asia/Tokyo", 3 | "dependencies": {}, 4 | "exceptionLogging": "STACKDRIVER", 5 | "runtimeVersion": "V8" 6 | } 7 | -------------------------------------------------------------------------------- /syncGoogleScriptRun.js: -------------------------------------------------------------------------------- 1 | /** 2 | * syncGoogleScriptRun for Javascript library 3 | * GitHub https://github.com/tanaikech/syncGoogleScriptRun
4 | * 5 | * Run google.script.run with the synchronous process. 6 | * @param {Object} resource the object for using syncGoogleScriptRun. 7 | * @return {Object} Returned value from the function of Google Apps Script side. 8 | */ 9 | const syncGoogleScriptRun = resource => { 10 | return new Promise((resolve, reject) => { 11 | google.script.run 12 | .withFailureHandler(e => reject(e)) 13 | .withSuccessHandler(e => resolve(e)) 14 | [resource.gasFunction](resource.arguments); 15 | }); 16 | }; 17 | -------------------------------------------------------------------------------- /syncGoogleScriptRun.min.js: -------------------------------------------------------------------------------- 1 | const syncGoogleScriptRun=resource=>new Promise((resolve,reject)=>{google.script.run.withFailureHandler(e=>reject(e)).withSuccessHandler(e=>resolve(e))[resource.gasFunction](resource.arguments)}); --------------------------------------------------------------------------------