javascript

Update: Javascript library - HtmlFormObjectParserForGoogleAppsScript_js

This is a Javascript library for sending the HTML form object to Google Apps Script using google.script.run. HtmlFormObjectParserForGoogleAppsScript_js was updated to v1.0.1. v1.0.1 (January 11, 2022) A new argument of includeOrder was added as the 4th argument. This library returns the parsed form object as a JSON object. By this, the order of HTML form object is not saved. From this version, this order can be included. This argument includes the order of each input tag in form.

Updated: Javascript library - ResumableUploadForGoogleDrive_js

ResumableUploadForGoogleDrive_js was updated to v2.0.0. v2.0.0 (November 15, 2021) New Class ResumableUploadToGoogleDrive2 was added. By this, the large file which is over the memory in the local PC can be uploaded by the resumable upload. Overview This is a Javascript library to achieve the resumable upload for Google Drive. Description When a file more than 5 MB is uploaded to Google Drive with Drive API, the resumable upload is required to be used.

Compiling Continuous Numbers using Google Apps Script

Gists This is a sample script for compiling the continuous numbers using Google Apps Script. For example, the values of [4, 5, 9, 3, 10, 5, 11, 7, 7, 13, 1] are converted to ["1","3-5","7","9-11","13"]. Sample script const compilingNumbers = (ar) => { const { values } = [...new Set(ar.sort((a, b) => a - b))].reduce( (o, e, i, a) => { if ( o.temp.length == 0 || (o.temp.length > 0 && e == o.

Using Google API Client Library (gapi) for JavaScript with Service Account

Gists This is a sample script for using Google API Client Library (gapi) for JavaScript with the service account. Unfortunately, in the current stage, gapi cannot directly use the service account. So, in this case, it is required to implement the script for retrieving the access token from the service account. In this report, I would like to introduce the method for using gapi with the service account using a Javascript library.

Javascript library - GetAccessTokenFromServiceAccount_js

Overview This is a Javascript library to retrieve the access token from the Google Service Account. Ref Description I have already posted the sample script for retrieving the access token from the Google Service Account. Ref But, when I use this script, I thought that when this was published as the Javascript library, it will be useful. So I created this. Install <script src="getaccesstokengromserviceaccount_js.min.js"></script> Or, using jsdelivr cdn <script src="https://cdn.jsdelivr.net/gh/tanaikech/GetAccessTokenFromServiceAccount_js@master/getaccesstokengromserviceaccount_js.min.js"></script> You can see the detail of this at https://github.

Javascript library - HtmlFormObjectParserForGoogleAppsScript_js

Parser for Sending HTML Form Object to Google Apps Script using google.script.run Overview This is a Javascript library for sending the HTML form object to Google Apps Script using google.script.run. Description HTML form object is parsed by this library, and the object can be sent to Google Apps Script using google.script.run. After the V8 runtime got to be able to be used for Google Apps Script, when the file input tag is included in the HTML form object, the object sent to Google Apps Script using google.

Safe-Uploading for Google Drive by HTML in External Server using Google Apps Script

Overview This is a report for safe-uploading files to Google Drive by HTML put in the external server using Google Apps Script. Description When you want to make the user upload a file to your own Google Drive using the HTML put in the external server of Google side, when the file size is smaller than 50 MB, this can be achieved without using the access token. Ref (When the HTML is put in the internal server of Google side, you can also use google.

Javascript library - BatchRequest_js

Overview This is a library for running Batch Requests for Google APIs using Javascript. Description When users use Google’s APIs, one quota is used for one API call. When the batch request is used, several APIs can be called by one quota, although there are some limitations in the batch request. google-api-javascript-client can run the batch request. Ref But, I created this for my self study. This library can achieve the batch request using fetch without using google-api-javascript-client.

Retrieving Access Token for Service Account using Javascript

Gists This is a sample script for retrieving the access token for Service Account using Javascript. The flow for using this script is as follows. At first, please create the Service Account and retrieve JSON file. Put Scopes, private_key and client_email to the script. Run the script. Sample script In this script, 2 libraries of jsencrypt and crypto-js are used. <script src="https://cdnjs.cloudflare.com/ajax/libs/jsencrypt/3.0.0-rc.1/jsencrypt.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.min.js"></script> <script> async function sample() { const private_key = "###"; // private_key of JSON file retrieved by creating Service Account const client_email = "###"; // client_email of JSON file retrieved by creating Service Account const scopes = ["https://www.

Uploading Image Files to Google Photos using axios

Gists This is a sample script for uploading the image files to the specific album in Google Photos using axios. Before you use this script, please retrieve the access token for uploading the files using Google Photos API. Sample script In this sample script, several image files can be uploaded. <input type="file" id="files" name="file" multiple /> <input type="button" onclick="main()" value="upload" /> <script> function upload({ files, albumId, accessToken }) { const description = new Date().

Sample Scripts for Creating New Event with Google Meet Link to Google Calendar using Various Languages

Gists This is the sample scripts for creating new event with Google Meet link to Google Calendar using various languages. When I saw the official document of “Add video and phone conferences to events”, in the current stage, I can see only the sample script for Javascript. But I saw the several questions related to this for various languages. So I published the sample scripts for creating new event with Google Meet link to Google Calendar using various languages.

Workaround: Showing Log in Web Apps to Apps Script Dashboard using Javascript

Gists I have already reported for showing the log to “Apps Script Dashboard” when it requests to the Web Apps. Ref In order to show the log to “Apps Script Dashboard” when it requests to the Web Apps, it is required to use the access token. But in the current stage, when the access token is used for XMLHttpRequest and fetch of Javascript in the request headers, the error related to CORS occurs.

Retrieving Difference Between 2 Arrays using Google Apps Script

Gists This is a sample script for retrieving the difference between 2 arrays, which are the old values and the new values, using Google Apps Script. In my environment, I sometimes have the situation that it is required to retrieve the difference between 2 arrays. So I prepared this as a sample script. I think that this can be also used at Javascript and Node.js. If this was also useful for your situation, I’m glad.

Updated: GetFileList for golang, Javascript, Node.js and Python

Updated: GetFileList for golang, Javascript, Node.js and Python This is the libraries to retrieve the file list with the folder tree from the specific folder of own Google Drive and shared Drives. Golang: https://github.com/tanaikech/go-getfilelist Updated to v1.0.4. Javascript: https://github.com/tanaikech/GetFileList_js Updated to v1.0.3. Node.js: https://github.com/tanaikech/node-getfilelist Updated to v1.0.5. Python: https://github.com/tanaikech/getfilelistpy Updated to v1.0.6.

Updated: Javascript library - GetFileList_js

GetFileList_js was updated to v1.0.2. v1.0.2 (May 15, 2020) Shared drive got to be able to be used. The file list can be retrieved from both your Google Drive and the shared drive. For example, when the folder ID in the shared Drive is used id of resource, you can retrieve the file list from the folder in the shared Drive. You can see the detail information here https://github.

Javascript library - ResumableUploadForGoogleDrive_js

Overview This is a Javascript library to achieve the resumable upload for Google Drive. Description When a file more than 5 MB is uploaded to Google Drive with Drive API, the resumable upload is required to be used. I have already published the sample script for “Resumable Upload for Web Apps using Google Apps Script”. Ref In this case, Web Apps is used. Here, I would like to introduce the script for the resumable upload created by only Javascript.

Javascript library - syncGoogleScriptRun

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.

Javascript library - GetFileList_js

Overview This is a Javascript library to retrieve the file list with the folder tree from the specific folder (publicly shared folders and own folders) of Google Drive. Description The library for retrieving the file list with the folder tree from the specific folder of Google Drive has already been published for Google Apps Script, golang, node.js and python as GetFileList. Ref Here, this GetFileList was released as the library of Javascript.

Upload Files to Google Drive using Javascript

Gists News At October 11, 2019, I published a Javascript library to to run the resumable upload for Google Drive. When this is used, the large file can be uploaded. You can also use this js library. Description This is a sample script for uploading files to Google Drive using Javascript. The files are uploaded by Drive API v3. gapi.client.drive.files.create() can create an empty file on Google Drive. But it cannot directly upload files including contents.

Adding Query Parameters to URL using Google Apps Script

Gists This is for adding the query parameters to the URL. This sample script is prepared by ES5. So this can be also used for Javascript. When I created an endpoint with some query parameters, I had used the scripts of various patterns every time. Today, I prepared this sample script to unify them. If this is also useful for you, I’m glad. Sample script : String.prototype.addQuery = function(obj) { return this + Object.

google.script.run and jQuery.Deferred

google.script.run doesn’t return values. So I tried this using jQuery.Deferred. GAS : Code.gs function doGet() { return HtmlService.createHtmlOutputFromFile('index') .setSandboxMode(HtmlService.SandboxMode.IFRAME); } function getValues(e) { return e + "hoge"; } HTML : index.html <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.js"></script> <body> <input type="button" id="button" value="ok"> <div id="result"></div> <script> $(() => { $("#button").click(() => { var str = "fuga"; googleScriptRun(str).then((res) => { $('#result').text(res); }); }); }); function googleScriptRun(str) { var d = new $.Deferred(); google.script.run.withSuccessHandler((res) => {d.resolve(res)}).getValues(str); return d.

Mixing 2 Array Objects Included Dictionary Object by Javascript

Gists This is a sample script for combining and mixing 2 objects. Each object is an array which included a dictionary type. When the key of the dictionary object is the same, the values are mixed. This can be also used for Google Apps Script. Input var obj1 = [ {"key1": ["value1a1", "value1a2"]}, {"key1": ["value1aa1", "value1aa2"]}, {"key2": ["value2a1", "value2a2"]}, {"key3": ["value3a1", "value3a2"]}, ]; var obj2 = [ {"key1": ["value1b1", "value1b2"]}, {"key3": ["value3b1", "value3b2"]}, {"key3": ["value3bb1", "value3bb2"]}, {"key4": ["value4b1", "value4b2"]}, ]; Output [ {"key1": ["value1a1", "value1a2", "value1b1", "value1b2", "value1aa1", "value1aa2"]}, {"key2": ["value2a1", "value2a2"]}, {"key3": ["value3a1", "value3a2", "value3b1", "value3b2", "value3bb1", "value3bb2"]}, {"key4": ["value4b1", "value4b2"]} ] Sample script : Javascript : function mixture(obj1, obj2) { Array.

Adding Object to Object by Javascript

Gists This sample script is for adding object to object by javascript. Script : var obj = { key1: "value1", key2: "value2", key3: "value3" }; var obj1 = { key4: "value4", key5: "value5", key6: "value6" }; Object.assign(obj, obj1); console.log(obj); Result : { key1: 'value1', key2: 'value2', key3: 'value3', key4: 'value4', key5: 'value5', key6: 'value6' } jsfiddle demo Reference : Object.assign()

Straightening Elements in 2 Dimensional Array using Google Apps Script

Gists This sample script is for straightening elements in 2 dimensional array using Google Apps Script (GAS). When applications using Spreadsheet are developed by GAS, it usually uses 2 dimensional array by setValues(). And the lengths of each element are required to be the same. On the other hand, data used for the applications might not be the same length for each element in 2 dimensional array. This sample script can be used under such situation.

Changing Values by Checking Duplicated Values of JSON for Javascript

Gists This sample script is for changing values by checking duplicated values of JSON for Javascript. Please see the following script. There is an array with a JSON data with 3 keys and 3 values. It is found that the values for each element duplicate. These duplicated values are changing by adding numbers. I use this for managing filenames. This script also can be used for Google Apps Script. If this was useful for you, I’m glad.

Retrieving Array Coordinates of Duplicated Elements

This sample is for retrieving array coordinates of duplicated elements. Script : var inputdata = ["a", "b", "b", "c", "d", "c", "e", "a", "f", "g"]; var dic = {}; var result = []; inputdata.forEach(function(e, i){ if (dic[e]) { result[i] = 'Duplicated'; } else { result[i] = null; } dic[e] = "temp"; }); Logger.log(JSON.stringify(result)) Logger.log([i for (i in result) if(result[i]=='Duplicated')]) Result : [null,null,"Duplicated",null,null,"Duplicated",null,"Duplicated",null,null] [2, 5, 7]

Removes Duplicate JSON Elements for a Value of a Certain Key

This sample removes duplicate JSON elements for a value of a certain key. When the value of the certain key is removed, only a first duplicate element is left. Also I had wanted to be used for Google Apps Script. So it became like this. Script : function removeDup(arr, key){ var temp = []; var out = []; arr.forEach( function (e, i) { temp[i] = (temp.indexOf(e[key]) === -1) ? e[key] : false; if (temp[i]) out.

Transposing Array From (n x m) To (m x n) for javascript

This script transposes from an array with n rows x m columns to the array with m rows x n columns. In this script, you can use array of n != m . array = [ [a1, b1, c1, d1, e1], [a2, b2, c2, d2, e2], [a3, b3, c3, d3, e3], [a4, b4, c4, d4, e4], [a5, b5, c5, d5, e5], [a6, b6, c6, d6, e6], [a7, b7, c7, d7, e7], [a8, b8, c8, d8, e8], [a9, b9, c9, d9, e9], [a10, b10, c10, d10, e10] ] var result = []; for (i in array[0]){ result.

Sample Array Script for Spreadsheet

This is a Sample Array Script for Spreadsheet. It makes an 2D array filled by strings and number. The strings and number are column strings and row number, respectively. However, because this is a sample, the maximum column number is 26. function sa(row, col){ if (col > 26) return; var ar = new Array(row); for(var i = 0; i < row; i++) ar[i] = new Array(col); for (var i = 0; i < row; i++){ for (var j = 0; j < col; j++){ ar[i][j] = String.