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

The Thinker

Modifying 1st-Page Header in Google Document using Google Apps Script

Gists

These are sample scripts for modifying the 1st-page header in Google Document using Google Apps Script. Unfortunately, in the current stage, the 1st-page header cannot be modified by Document service. In this case, it is required to use Google Docs API. Here, I would like to introduce 2 sample scripts for modifying the 1st page header using Docs API.

When you use this, please enable Google Docs API at Advanced Google services.

Uploading File to Google Drive from External HTML without Authorization

Gists

This is a sample script for uploading a file to Google Drive from the external HTML without the authorization. In this case, the client side can be used at the outside of Google. And as the server side, the Web Apps created by Google Apps Script is used.

Usage

Please do the following flow.

1. Create new project of Google Apps Script.

Sample script of Web Apps is a Google Apps Script. So please create a project of Google Apps Script.

Decoding QR code on Google Slides using Google Apps Script

Gists

This is a sample script for decoding a QR code put in Google Slides using Google Apps Script. In this case, Javascript is used at the opened dialog. And Canvas API and jsQR are used. So unfortunately, this method cannot be used with the time-driven trigger and the Google Apps Script project of the standalone type.

Of course, this method can be also used for Google Document and Google Spreadsheet. But at Google Spreadsheet, I recommend to retrieve the image of QR code with the fetch method from URL.

Cropping Images in Google Slides using Google Apps Script

Gists

This is a sample script for cropping images in the Google Slides using Google Apps Script. In the current stage, in order to crop the images in Google Slides, it is required to use replace(blobSource, crop) Because, although there is the “cropProperties” of “UpdateImagePropertiesRequest” in Slides API, unfortunately, in the current stage, this cannot be still used. This has already been reported. Ref

About cropping using replace(blobSource, crop), I thought that how to use might be a bit difficult. So here, I would like to introduce a sample script for using replace(blobSource, crop).

IMPORTANT: reduceRight with and without v8 runtime for Google Apps Script

Gists

This is an important point for using reduceRignt with and without v8 runtime for Google Apps Script.

Sample script

function myFunction() {
  var array = ["a", "b", "c", "d", "e"];
  var res = array.reduceRight(function (ar, e, i) {
    ar.push([e, i]);
    return ar;
  }, []);
  Logger.log(res);
}

Result

With V8

When V8 runtime is used, the following result is obtained.

[["e",4],["d",3],["c",2],["b",1],["a",0]]

Without V8

When V8 runtime is NOT used, the following result is obtained.

[["e",0],["d",1],["c",2],["b",3],["a",4]]

Summary

When above results are compared, it is found that the indexes are different. The indexes of “With V8” is in the opposite direction to that of “Without V8”. Please be careful this.

GAS Library - GASProjectApp

Overview

This is a Google Apps Script library for creating, updating and exporting Google Apps Script project of the standalone type using Drive API. In this case, Apps Script API is not used.

Description

I had reported “Drive API cannot create Google Apps Script project no longer” before. Ref About this, I had reported the future request. Ref At July 30, 2020, I could confirm that the Google Apps Script project of the standalone type got to be able to be created by multipart/form-data using Drive API again. Ref This is a good news for me. By this, in order to use this with Google Apps Script, I created this library. Because in this case, when the update method is used, the special scope of https://www.googleapis.com/auth/drive.scripts is required. So I thought that when this is published as the Google Apps Script library, this will be useful for users.

Drive API got to be able to create Google Apps Script project again

Gists

I have reported “Drive API cannot create Google Apps Script project no longer”. Ref About this, I had reported the future request. Ref Today, I could confirm that the Google Apps Script project of the standalone type got to be able to be created by multipart/form-data using Drive API. This is a good news for me. By this, the following 2 patterns can be used from now.

Pattern 1:

  1. Create new standalone GAS project by Apps Script API.
  2. Put the local script to the created GAS project by updating the project with Apps Script API.
  3. Move the GAS project from the root folder to the specific folder using Drive API.

In this pattern, 3 API calls are required.

Uploading Files of multipart/form-data to Google Drive using Drive API with Node.js

Gists

These are the sample scripts for uploading files of multipart/form-data to Google Drive using Drive API with Node.js. In this case, googleapis for Node.js is not used.

In these sample script, the maximum file size is 5 MB. Please be careful this. When you want to upload the files more than 5 MB, please check this report.

Sample script 1

This sample script uploads a file using the modules of fs and request. Before you use this script, please prepare your access token for uploading the file.