tanaike - Google Apps Script, Gemini API, and Developer Tips

The Thinker

Trend of google-apps-script Tag on Stackoverflow 2021

Gists

Published: January 16, 2021

Kanshi Tanaike

Introduction

At Stackoverflow, a lot of people post the questions and answers to the questions every day. There are various tags in Stackoverflow. A lot of discussions are performed at each tag. Their discussions bring the important information and are much useful for a lot of people. As one of tags, there is “google-apps-script”. I sometimes discuss at the questions with that tag. When we see the discussions, we can notice that the discussions are changed and progressed by the time, because “Google Apps Script” which is the origin of the tag is updated. This report thinks this change as the trend of tag of “google-apps-script”. This trend includes the number of questions, questioners, answerers and tags adding to the tag of “google-apps-script”. The trend of tag of “google-apps-script” is deeply related to the progression of Google Apps Script and the various applications for Google Apps Script.

Creating Custom Grid View of Google Slides as Image and Spreadsheet using Google Apps Script

Gists

This is a sample script for creating the custom grid view of Google Slides as an image using Google Apps Script.

Demo

Usage

In order to use this script, please do the following flow.

1. Install GAS library

This sample script uses a library of DocsServiceApp. So please install DocsServiceApp. You can see the method for installing it at here.

2. Enable APIs

This sample script uses 2 APIs of Drive API and Slides API. So please enable them at Advanced Google services. Ref

Adding Slide Page Link to Shape using Google Apps Script

Gists

This is a sample script for adding the slide page link to the shape using Google Apps Script.

When I use Google Slides, there is the case that I want to jump to the specific slide on the same Google Slides. And, I have the case that I want to jump from the last slide to the 1st slide. In those cases, I had manually added the slide page link to each shape. But when the number of slides are large, I thought that when a script for achieving this is prepared, it will be useful. So I created this.

Simple Script of Resumable Upload with Google Drive API for Axios

Gists

This is a simple sample script for achieving the resumable upload to Google Drive using Axios. In order to achieve the resumable upload, at first, it is required to retrieve the location, which is the endpoint of upload. The location is included in the response headers. After the location was retrieved, the file can be uploaded to the location URL.

In this sample, a text data is uploaded with the resumable upload using a single chunk.

Downloading and Uploading File to Google Drive without Saving File with Stream and Resumable Upload using Node.js

Gists

This is a sample script of Node.js for downloading the data and uploading the data to Google Drive with the resumable upload without saving it as a file. The downloaded data is uploaded to Google Drive with the stream.

Sample script

Before you use this, please set the variables of accessToken, url, fileSize, mimeType and filename. In this case, fileSize is required to set because the data is uploaded with the resumable upload.

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.script.run.) But, when the file size is over 50 MB, it is required to upload the file with the resumable upload. In this case, the access token is required to be used. In this case that the user uploads to your own Google Drive, when the access token is used in the upload, it is considered that this is the weak point of the security. In this report, I would like to propose the method for safe-uploading files to Google Drive by HTML put in the external server using Google Apps Script. Please think of this as one of several methods.

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.

  1. At first, please create the Service Account and retrieve JSON file.
  2. Put Scopes, private_key and client_email to the script.
  3. 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.googleapis.com/auth/drive.readonly"]; // Scopes

    const url = "https://www.googleapis.com/oauth2/v4/token";
    const header = { alg: "RS256", typ: "JWT" };
    const now = Math.floor(Date.now() / 1000);
    const claim = {
      iss: client_email,
      scope: scopes.join(" "),
      aud: url,
      exp: (now + 3600).toString(),
      iat: now.toString(),
    };
    const signature =
      btoa(JSON.stringify(header)) + "." + btoa(JSON.stringify(claim));
    const sign = new JSEncrypt();
    sign.setPrivateKey(private_key);
    const jwt =
      signature + "." + sign.sign(signature, CryptoJS.SHA256, "sha256");
    const params = {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({
        assertion: jwt,
        grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer",
      }),
    };
    const obj = await fetch(url, params)
      .then((res) => res.json())
      .catch((err) => console.log(err));
    console.log(obj);
  }

  sample();
</script>

If the access token retrieved at above is used for retrieving file list, the sample script is as follows.

Meet the Google Workspace Developer Experts

Original post

Superheroes are well known for wearing capes, fighting villains and looking to save the world from evil. There also are superheroes that quietly choose to use their super powers to explain technology to new users, maintain community forums, write blog posts, speak at events, host video series, create demos, share sample code and more. All in the name of helping other developers become more successful by learning new skills, delivering better apps, and ultimately enhancing their careers. At Google, we refer to the latter category of superheroes as Google Developer Experts or “GDEs” for short.