Gists
Abstract This report showcases a practical application of Google Apps Script, demonstrating how new JavaScript methods can be used to create a script that automatically transfers selected rows between sheets in a Google Sheet.
Introduction JavaScript, a fundamental pillar of contemporary web development, has experienced a significant rise in popularity due to its versatility and widespread adoption. As JavaScript’s influence has expanded, so too has Google Apps Script, a cloud-based scripting language constructed on the V8 JavaScript engine.
UtlApp was updated to v1.0.7. v1.0.7 (September 4, 2024)
Following 3 methods were added. snake_caseToCamelCase: This method is used for converting a string of the snake case to the camel case. camelCaseTosnake_case: This method is used for converting a string of the camel case to the snake case. createFormDataObject: This method is used for creating the form data to HTTP request from an object. You can see the detail information here https://github.
Gists
Overview This sample script demonstrates uploading multiple files using split asynchronous processes with resumable upload. It leverages JavaScript and HTML within Google Spreadsheets.
Description In my previous report, “Resumable Upload of Multiple Files with Asynchronous Process for Google Drive”, I presented an approach for uploading files asynchronously.
This script builds upon that concept, introducing a method for uploading multiple files with split asynchronous processes that utilize resumable upload.
Here’s the process breakdown:
TriggerApp was updated to v1.0.3. v1.0.3 (June 26, 2024)
The calculation for increasing the month was modified. A new scenario 8 was added. In scenario 8, you can see how to use the month-end. Ref You can see the detail information here https://github.com/tanaikech/TriggerApp
ResumableUploadForGoogleDrive_js was updated to v2.0.2.
v2.0.2 (May 23, 2024)
From this version, the files could be also uploaded to the shared drive. CDN Class ResumableUploadToGoogleDrive This Class can achieve the resumable upload of a file by reading the file to the memory.
<script src="https://cdn.jsdelivr.net/gh/tanaikech/ResumableUploadForGoogleDrive_js@2.0.2/resumableupload_js.min.js"></script> Class ResumableUploadToGoogleDrive2 This Class can achieve the resumable upload of a file by directly reading partially the file from the local Disk.
PDFApp was updated to v1.0.7. v1.0.7 (May 15, 2024)
The method of “addPageNumbers” was updated. Ref When a number is used to the property x instead of “left”, “center”, and “right”, the inputted number is directly used. You can see the detail information here https://github.com/tanaikech/PDFApp
PDFApp was updated to v1.0.6. v1.0.6 (May 15, 2024)
A new method of “addPageNumbers” was added. Ref This method adds the page numbers to each page of the PDF. You can see the detail information here https://github.com/tanaikech/PDFApp
Gists
Description This is a simple sample script for adding the page numbers to PDF data using Google Apps Script.
When you use this script, please copy and paste the following script to the script editor of Google Apps Script. And, please set the file ID of the PDF file.
Sample script In this script, pdf-lib is used.
/** * ### Description * Add page numbers to PDF. * * @param {Object} blob PDF blob.
UtlApp was updated to v1.0.6. v1.0.6 (May 4, 2024)
Methods of blobToDataUrl was added. When this method is used, the Blob data can be converted to the data URL. You can see the detail information here https://github.com/tanaikech/UtlApp
UtlApp was updated to v1.0.5. v1.0.5 (April 16, 2024)
Methods of consolidateA1Notations was added. When this method is used, the scattered A1Notations can be consolidated. You can see the detail information here https://github.com/tanaikech/UtlApp
UtlApp was updated to v1.0.4. v1.0.4 (April 13, 2024)
Updated the methods of convGridRangeToA1Notation. When the sheet name is not given, only the A1Notation is returned. You can see the detail information here https://github.com/tanaikech/UtlApp
PDFApp was updated to v1.0.5. v1.0.5 (February 5, 2024)
A new method of “splitPDF” was added. Ref This method splits each page of a PDF to an individual PDF file. You can see the detail information here https://github.com/tanaikech/PDFApp
PDFApp was updated to v1.0.4. v1.0.4 (February 5, 2024)
From this discussion, I changed the logic of copyPages. You can see the detail information here https://github.com/tanaikech/PDFApp
UtlApp was updated to v1.0.3. v1.0.3 (February 5, 2024)
Methods of addQueryParameters was updated. You can see the detail information here https://github.com/tanaikech/UtlApp
UtlApp was updated to v1.0.2. v1.0.2 (January 25, 2024)
2 methods of dotProduct and cosineSimilarity were added. You can see the detail information here https://github.com/tanaikech/UtlApp
PDFApp was updated to v1.0.3. v1.0.3 (November 26, 2023)
From this discussion, I changed the logic of the method mergePDFs. The method for using mergePDFs and the output are not changed. With this modification, the large PDF data can be merged. You can see the detail information here https://github.com/tanaikech/PDFApp
Gists
Abstract One day, you might have a situation where you are required to create a Web Apps with Google Apps Script and are required to load another HTML created by Javascript on the Web Apps. This report will help achieve such a situation.
Introduction Google Apps Script can create Web Apps. Ref When you access the Web Apps using your browser, you can see the HTML. When your browser can run Javascript, you can see the HTML reflecting the Javascript.
Overview This is a Google Apps Script library for managing the histories of the Google Apps Script project.
Description On August 23, 2023, the project history has been implemented in the new IDE of Google Apps Script. Ref and Ref In the current stage, the users can see the history of the previously deployed script version. This is a very important implementation for a lot of developers.
Here, I remember the classic IDE.
Gists At the Google Apps Script project, the values can be transferred from HTML to Google Apps Script using google.script.run with Javascript. In this case, unfortunately, the values of all types cannot be transferred. In the official document, it says as follows. Ref Most types are legal, but not Date, Function, or DOM element besides form; see description Legal parameters are JavaScript primitives like a Number, Boolean, String, or null,
PDFApp was updated to v1.0.2. v1.0.2 (August 21, 2023)
A new method of insertHeaderFooter was added. Ref When this method is used, the custom header and footer can be added when a Google Spreadsheet is exported as PDF. You can see the detail information here https://github.com/tanaikech/PDFApp
Gists
This is a sample script for adding header and footer to PDF using Google Apps Script.
In the current stage, when Google Spreadsheet is manually exported as a PDF file at “Print settings” on the UI of Spreadsheet, the custom header and footer can be added as shown in the following image.
But, unfortunately, in the current stage, this cannot be directly achieved by Google Apps Script. So, I created this sample script.
PDFApp was updated to v1.0.1. v1.0.1 (August 18, 2023)
About the method of “getMetadata”, pageInfo is added to the retrieved metadata. By this, each page size can be obtained. You can see the detail information here https://github.com/tanaikech/PDFApp
TriggerApp was updated to v1.0.1. v1.0.1 (August 18, 2023)
When toDay is not used, there was a case that the next trigger is not installed. This bug was removed. You can see the detail information here https://github.com/tanaikech/TriggerApp
Overview This is a Google Apps Script library for managing PDFs.
Description Google Apps Script is one of the most powerful tools for cloud computing. When Google Apps Script is used, the result can be obtained even when the user doesn’t stay in front of the PC and mobile phone by the triggers. One day, there might be a case where it is required to manage PDF data using Google Apps Script.
Gists
Abstract When PDF file can be managed with Google Apps Script, that will lead to the automation process on cloud. In this report, the method for cooking PDF over Google Apps Script.
Introduction Google Apps Script is one of the strong tools for achieving the automation process. When Google Apps Script can be used for the situation, it can be processed with cloud computing. By this, the users are not required to stay on the desks with the PC.
Gists
This is a sample script for embedding the objects in PDF using Google Apps Script.
Recently, I had a situation where it is required to manage PDFs using Google Apps Script. At that time, I had a situation where it is required to embed objects of texts and images in PDF using Google Apps Script. So, I created the following Class with Google Apps Script. When this Class is used, the objects of texts and images can embed in PDF.
Gists This is a sample script for creating PDF forms from a Google Slide template using Google Apps Script. Recently, I had a situation where it is required to create a custom PDF form. In that case, I thought that when a PDF form can be created from a template, it might be useful. So, I created the following Class with Google Apps Script. When this Class is used, a
Gists This is a sample script for retrieving and putting values for PDF Forms using Google Apps Script. PDF can have the PDF Form for inputting the values in the PDF by the user. Ref Recently, I had a situation that required me to retrieve and put the values to the PDF Form using Google Apps Script. In order to achieve this, I created a Class object with Google Apps
Gists
This is a sample script for changing the order of pages in a PDF file using Google Apps Script.
Sample script Before you run this script, please set the variables in the function main.
/** * ### Description * Changing order of pages in a PDF file. * * @param {Object} fileId is file ID of PDF file. newOrderOfpages is new order of pages. About "ignoreSkippedPages", if this is false, when the PDF has 5 pages and "newOrderOfpages" is "[3, 2]", the exported PDF file has 5 pages of 3, 2, 1, 4, 5.
Gists
This is a sample script for managing the metadata of PDF data using Google Apps Script.
There might be a case in that you want to retrieve and update the metadata of PDF data using Google Apps Script. In this post, I would like to introduce achieving this.
Class ManagePdfMetadata This is a Class ManagePdfMetadata. This Class is used for managing the metadata of PDF files using Google Apps Script.
Overview This is a Google Apps Script library for efficiently managing the time-driven triggers for executing Google Apps Script using Google Apps Script.
Description Google Apps Script can execute with not only the manual operation but also several triggers. The time-driven trigger is one of them, and this is one of a lot of important functions. When the time-driven trigger is used, Google Apps Script can be automatically executed at the time you set without launching the user’s PC.
Gists
This is a sample script for exporting the specific pages from a PDF as a new PDF using Google Apps Script.
In this sample script, pdf-lib is used. In the current stage, it seems that this Javascript can be directly used with Google Apps Script.
Sample script async function myFunction() { // Retrieve PDF data. const fileId = "###"; // Please set a file ID of your a PDF file or a file ID of Google Docs files (Document, Spreadsheet, Slide).
Overview This is a Google Apps Script library for easily managing the template of Google Documents and Google Slides using Google Spreadsheet as a database using Google Apps Script.
Description You might have situations where are required to create multiple Google Documents and Google Slides from the templates using Google Spreadsheet as a database with Google Apps Script. When the simple texts are replaced with the placeholders on the templates, this can be achieved by a simple script.
Overview This is a Google Apps Script library including useful scripts for supporting to development of applications by Google Apps Script. In the current stage, the 3 categories “For array processing”, “For binary processing”, and “For string processing” are included in this library.
Description When I create applications using Google Apps Script, there are useful scripts for often use. At that time, I thought that when those scripts can be simply used, they will be useful not only to me but also to other users.
Gists
Today, I discussed with Riƫl Notermans an issue with the HTML form with the input tab of type="file" with google.script.run. Through this discussion, the reason for this issue could be found. When you use the input tab of type="file" in the HTML form, and you want to send the file content with google.script.run, I thought that this post might be useful for other users. So, I posted it here.
Gists
This sample script decrypts the salted base64 data of finance.yahoo.com using Google Apps Script.
Recently, it seems that the specification of the key for decrypting the data has been changed on the server side, again. In this update, I looked for the logic for retrieving the key value. But, I cannot still find it. So, in this post, I would like to use a workaround discussed in this thread. In this thread, the valid keys are listed in a text file.
Gists
This sample script decrypts the salted base64 data of finance.yahoo.com using Google Apps Script.
Recently, it seems that the specification of the key for decrypting the data has been changed at the server side. So. from this script, I updated the script as follows.
Sample script function myFunction() { // Load crypto-js.min.js. const cdnjs = "https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"; eval(UrlFetchApp.fetch(cdnjs).getContentText()); // Retrieve HTML and retrieve salted base64.
Gists
This is a sample script for checking and replacing a character of U+00A0 (no-break space) with U+0020 (space) as Unicode using Google Apps Script.
When I’m seeing the questions on Stackoverflow, I sometimes saw the situation that the script doesn’t work while the script is correct. In this case, there is the case that the reason is due to U+00A0 being used as the spaces. When U+00A0 is used as the spaces, Google Apps Script and formulas cannot be correctly run.
Gists
This sample script decrypts the salted base64 data of finance.yahoo.com using Google Apps Script.
Recently, it seems that the specification of the key for decrypting the data has been changed at the server side. So. in this post, this post is updated. About this specification, I checked this thread.
Sample script function myFunction() { // Load crypto-js.min.js. const cdnjs = "https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"; eval(UrlFetchApp.fetch(cdnjs).getContentText()); // Retrieve HTML and retrieve salted base64.
Gists
This is a sample script for merging multiple PDF files as a single PDF file using Google Apps Script.
In this sample script, pdf-lib is used. In the current stage, it seems that this Javascript can be directly used with Google Apps Script.
Sample script 1 As a sample situation, please put multiple PDF files in your Google Drive. This sample merges those PDF files as a single PDF file.
Gists
This is a sample script for retrieving the start and end of the month in a year using Google Apps Script and Javascript.
Sample script function myFunction() { const year = 2023; // Please set year you expect. const res = [...Array(12)].map((_, i) => [0, 1].map((e) => new Date(year, i + e, 1 - e)) ); console.log(res); console.log(res.map(([a, b]) => [a.toDateString(), b.toDateString()])); } Testing https://jsfiddle.
Gists
This is a simple sample script for filtering JSON objects using Google Apps Script.
In the current stage, V8 runtime can be used with Google Apps Script. By this, when you want to filter a JSON object, you can use the following sample script.
Sample script In this sample script, obj is filtered by the value of the even number.
const obj = { key1: 1, key2: 2, key3: 3, key4: 4, key5: 5 }; const res = Object.
Gists
This sample script decrypts the salted base64 data of finance.yahoo.com using Google Apps Script.
Recently, when I saw the HTML of finance.yahoo.com, I noticed that the data is converted by the salted base64. In order to decrypt the data, it is required to use the key data. But, unfortunately, I couldn’t find the key data from the HTML. When I searched for it, I found this thread. From the thread, I could retrieve the key data.
Gists
This is a sample script for encrypting and decrypting with AES using crypto-js with Google Apps Script.
Unfortunately, in the current stage, Google Apps Script cannot encrypt and decrypt AES using the built-in functions. In this post, in order to achieve this, “crypto-js” is used from cdnjs.com ( https://cdnjs.com/libraries/crypto-js ). In the current stage, it seems that the main functions of crypto-js.min.js can be directly used with Google Apps Script.
Gsits
This is a sample script for retrieving the values from a publicly shared Google Spreadsheet using an API key with Javascript.
Sample script In this sample script, googleapis for Javascript is used.
<script async defer src="https://apis.google.com/js/api.js" onload="handleClientLoad()"></script> <script> function handleClientLoad() { const apiKey = "###"; // Please set your API key. const spreadsheetId = "###"; // Please set your Spreadsheet ID. gapi.load('client', async () => { await gapi.
Gists
This is a sample script for updating Array1 with Array2 using Google Apps Script.
As a sample situation, there are 2 arrays (Array1 and Array2) of the 2-dimensional array. The sample situation can be seen in the above sample Spreadsheet.
Conditions When the values of column “A” of Array2 are existing in column “A” of Array1, the rows of Array1 are updated by that of Array2. When the values of column “A” of Array2 are not existing in column “A” of Array1, the rows of Array2 are appended to Array1.
Gists
This is a report related to the rule of item IDs for questions of Google Forms.
When the questions are created using the method of batchUpdate with Google Forms API, the created questions have the item IDs when the item IDs are not given in the request body. ( https://developers.google.com/forms/api/reference/rest/v1/forms#item ) For example, when you want to create a question and update the created question in one API call, it is required to include the custom item ID in the request body.
Javascript Library for Cropping Image by Border Overview This is a Javascript library for cropping images by the border.
Description When an image is used, there is a case where I wanted to simply crop the image by a script. In this Javascript library, the image is cropped by a border. The sample situation is as follows.
In this sample situation, a red rectangle is enclosed by a border (1 pixel) with “#000000”.
Gists
This is a sample script for splitting and processing an array every n length using Google Apps Script. When I prepare a sample script with Google Apps Script, I sometimes have the situation that it is required to split and process an array every n length. This sample script is for achieving this situation.
Please set limit. This sample script splits the sample array every 3 length.
When you use this script with Google Apps Script, please enable V8 runtime.
Gists
This is a sample script for increasing the column letter by one using Google Apps Script.
Sample script const increase = (ar) => ar.map((e) => { const idx = [...e].reduce( (c, e, i, a) => (c += (e.charCodeAt(0) - 64) * Math.pow(26, a.length - i - 1)), -1 ); // Ref: https://stackoverflow.com/a/53678158 columnIndexToLetter = (n) => (a = Math.floor(n / 26)) >= 0 ?
Gists
Introduction In this report, I would like to introduce to apply the spread syntax and the destructuring assignment to Google Spreadsheet with Google Apps Script. The destructuring assignment can be used without V8 runtime. But, the spread syntax is required to be used with V8 runtime. Recently, I often saw the script using them at Stackoverflow. And also, I have sometimes gotten the questions related to the spread syntax and the destructuring assignment.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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().
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.
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.
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
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.
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.
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.
GetFileList_js was updated to v1.0.1. v1.0.1 (September 30, 2019)
A bug related to the scope of variables was removed. You can see the detail information here https://github.com/tanaikech/GetFileList_js
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.
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.
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.
Gists
Updated on February 5, 2024 This is for adding the query parameters to the URL. These scripts 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 (With V8 runtime): String.prototype.addQuery = function (obj) { return (this == "" ?
Gists
This sample script is for retrieving a key with the maximum value from an object. This can be also used by Google Apps Script.
var obj = {"a": 5, "b": 4, "c": 3, "d": 2, "e": 1}; var res = Object.keys(obj).reduce(function(a, c) { return obj[a] > obj[c] ? a : c; }); Logger.log(res); // >>> a
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 $.
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.
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.
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.
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.
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]
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.
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.
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.