Skip to content

Instantly share code, notes, and snippets.

@tanaikech
tanaikech / submit.md
Last active Oct 15, 2020
Dynamically Updating Custom Menu of Google Spreadsheet using Google Apps Script
View submit.md

Dynamically Updating Custom Menu of Google Spreadsheet using Google Apps Script

This is a sample script for dynamically updating the custom menu of Google Spreadsheet using Google Apps Script.

Demo

In this demonstration, when the Spreadsheet is opened, 5 functions added to the custom menu. You can see that when a column is added and deleted, the custom menu is updated.

@tanaikech
tanaikech / submit.md
Last active May 4, 2021
Enhanced makeCopy() using Google Apps Script
View submit.md

Enhanced makeCopy() using Google Apps Script

Overview

This is sample scripts for copying files to a specific folder in Google Drive using Google Apps Script (GAS).

Description

When the files in Google Drive are copied to a specific folder using GAS, most users will use makeCopy(destination). When the files are copied using makeCopy(), you might have already noticed that only the standalone projects cannot be copied to the specific folder. They are copied to the root folder (My Drive). Also this situation can be also seen even when "copy" of Drive API v2 and v3 is used. I think that this might be a bug. So I would like to introduce 2 sample scripts as the workaround. In the workaround, it uses Drive API.

Sample script 1

This sample is very simple.

@tanaikech
tanaikech / submit.md
Created Aug 20, 2018
Replacing Text to Image for Google Document using Google Apps Script
View submit.md

Replacing Text to Image for Google Document using Google Apps Script

This is a sample script for replacing text to image for Google Document using Google Apps Script (GAS). There is a method for replacing text to text at Class Text of DocumentApp. But there are not methods for replacing text to image. So I created this sample script.

Demo :

This sample image was created by k3-studio.

Usage :

@tanaikech
tanaikech / submit.md
Last active Jun 4, 2021
Adding Query Parameters to URL using Google Apps Script
View submit.md

Adding Query Parameters to URL using Google Apps Script

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.keys(obj).reduce(function(p, e, i) {
    return p + (i == 0 ? "?" : "&") +
      (Array.isArray(obj[e]) ? obj[e].reduce(function(str, f, j) {
        return str + e + "=" + encodeURIComponent(f) + (j != obj[e].length - 1 ? "&" : "")
@tanaikech
tanaikech / submit.md
Last active Jan 22, 2021
Benchmark: fetchAll method in UrlFetch service for Google Apps Script
View submit.md

Benchmark: fetchAll method in UrlFetch service for Google Apps Script

By Google's update at January 19, 2018, fetchAll method was added to the UrlFetch service. When I saw the usage, I couldn't find the detail information about the actual running state. So I investigated about it.

As the result, it was found that the fetchAll method is worked by the asynchronous processing. The returned data is reordered by the order of requests. By this, it was also found that if you want to retrieve the data from the several URL, the process cost of UrlFetchApp.fetchAll() is much lower than that of UrlFetchApp.fetch() using for loop.

The sample scripts for server side and client side are as follows.

Sample script for server side

In this report, 5 Web Apps were used as the servers. At first, 5 standalone projects were created and the following server script was put to each project. Then, Web Apps was deployed for ea

@tanaikech
tanaikech / submit.md
Last active Dec 30, 2019
Retrieving ClientId using Google Apps Script
View submit.md

Retrieving ClientId using Google Apps Script

This is a sample script for retrieving clientId using Google Apps Script.

var accessToken = ScriptApp.getOAuthToken();
var url = "https://www.googleapis.com/oauth2/v3/tokeninfo";
var params = {
  method: "post",
  headers: {"Authorization": "Bearer " + accessToken}
};