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

The Thinker

Updated: GAS Library - ImgApp

ImgApp was updated to v1.1.0. New method was added.

2. doResize()

Overview

This method is for resizing images.

Description

Unfortunately, there are no methods to resize images at Google Apps Script. As a workaround, there is a method that it imports the image in Google Document and resizes the image using setWidth() and setHeight(). But in this method, the resized blob cannot be retrieved. So although I had thought of other workaround, I had not been able to find it. Recently, I accidentally discovered the other workaround doResize(). Since it was found that this workaround can be surely used, I added this to ImgApp.

GAS Library - ImgApp - getSize()

1. getSize()

Overview

This method is for retrieving the width and height of image as the unit of pixel.

Description

Unfortunately, there are no methods to directly retrieve the image size at Google Apps Script. As a workaround, there is a method that it imports the image in Google Document and retrieves the size using getWidth() and getHeight(). But in this method, it uses much time and resources on Google. So I thought of retrieving the information of image at the binary level, and created this. By this, the low process cost could be achieved.

Sublime Text Plugin - gislacks

Overview

This is a plugin of Sublime Text 3 for submitting files to both Gist and Slack.

Description

I like to use Sublime Text for developing scripts. And when I discuss about developing scripts, I often use Slack. When I submitted a script to Slack, I had saved the script to Gist as a backup. I had done manually this on my browser. Namely, I wanted to be saving the revision of script while I’m discussing about the script at Slack. One day, I wished this process had been able to be automatically run. So I created this plugin and gislack of a CLI tool.

CLI Tool - gislack

Overview

This is a CLI tool to submit files to both Gist and Slack.

Description

When I discuss about developing scripts, I often use Slack. When I submitted a script to Slack, I had saved the script to Gist as a backup. I had done manually this on my browser. Namely, I wanted to be saving the revision of script while I’m discussing about the script at Slack. Recently, I wished this process had been able to be automatically run. So I created this tool.

Go Library - getcode

Overview

This is a Golang library to automatically get an authorization code for retrieving access token using OAuth2.

Description

When it retrieves an access token and refresh token using OAuth2, the code for retrieving them has to be got by authorization on own browser. In order to retrieve the code, in generally, users have to click the authorization button and copy the code on the browser. This library can be automatically got the code by launching HTML server as a redirected server. At first, I have used this for retrieving the code from Google. But recently I noticed that this can be used for other sites. They are Google, GitHub, Slack and so on. This library can be used for creating such applications.

Retrieving Access Token for Google APIs

Gists

This sample is for retrieving access token for Google APIs. I created this for studying newStateToken().

Preparation

In order to use this sample, please do as follows.

  1. Deploy and launch Web Apps for retrieving redirect uri
    • On the Script Editor
      • File
      • -> Manage Versions
      • -> Save New Version
      • Publish
      • -> Deploy as Web App
      • -> At Execute the app as, select “your account”
      • -> At Who has access to the app, select “Only myself”
      • -> Click “Deploy”
      • -> Click “latest code” (By this click, it launches the authorization process.)
      • -> Please copy URL shown in the top of your browser as the redirect URI. And please modify the redirect URI like https://script.google.com/macros/s/#####/usercallback.
  2. Open console project
    • On the Script Editor
      • -> Resources
      • -> Cloud Platform Project
      • -> Click “Projects currently associated with this script”
      • -> Click API in start guide
  3. Retrieve client id and client secret
    • On the Console Project
      • Click authentication information at left side
      • -> Create a valid Client ID as OAyth client ID
      • -> Choose Web Application
      • -> Input Name (This is a name you want.)
      • -> Input redirect URI that you have already copied.
      • -> done
      • -> Please copy client ID and client Secret in a pop-up window.

Here, you have client ID, client Secret and redirect URI to retrieving refresh token and access token. These can be used for following sample script.

Updated: GAS Library - SOUWA

SOUWA means summing in Japanese. SOUWA can sum string elements in an array at the high speed. The speed of SOUWA with the pyramid algorithm is about 380 times faster than that of the standard method. New algorithm for summing array elements was developed for SOUWA. You can see the detailed report of this library at here. If you are interested in this, I’m glad.

It was updated to v1.0.2. Please check it out. https://github.com/tanaikech/SOUWA_GAS

Benchmark: Retrieving Values from Deep Nested JSON at Golang

This sample script is for retrieving values from a deep nested JSON. There are 2 patterns. So for these, the benchmark were measured.

Script :

package main

import (
    "encoding/json"
    "testing"
)

const (
    data = `{
      "A_key1": {
        "B_key1": {
          "C_key": "value"
        }
      }
    }`
)

func BenchmarkB1(b *testing.B) {
    b.ResetTimer()
    for i := 0; i < b.N; i++ {
        var p map[string]interface{}
        json.Unmarshal([]byte(data), &p)
        a1 := p["A_key1"]
        a2 := p["A_key1"].(map[string]interface{})["B_key1"]
        a3 := p["A_key1"].(map[string]interface{})["B_key1"].(map[string]interface{})["C_key"]
        _ = a1 // --> map[B_key1:map[C_key:value]]
        _ = a2 // --> map[C_key:value]
        _ = a3 // --> value
    }
}

func BenchmarkB2(b *testing.B) {
    b.ResetTimer()
    for i := 0; i < b.N; i++ {
        var p map[string]interface{}
        json.Unmarshal([]byte(data), &p)
        b1 := p["A_key1"]
        temp, _ := json.Marshal(b1)
        json.Unmarshal(temp, &p)
        b2 := p["B_key1"]
        temp, _ = json.Marshal(b2)
        json.Unmarshal(temp, &p)
        b3 := p["C_key"]
        _ = b1 // --> map[B_key1:map[C_key:value]]
        _ = b2 // --> map[C_key:value]
        _ = b3 // --> value
    }
}

Result :

$ go test -bench .
BenchmarkB1-4             300000              4177 ns/op
BenchmarkB2-4             100000             13619 ns/op
PASS

It was found that the process cost of json.Unmarshal() was high. json.Unmarshal() for test 2 is 3 times larger than that for test 1.

Reopening Current File as a File with New Name at Sublime

This is for Sublime Text. This sample is for reopening current file as a file with new file name. The current file is closed when reopening a new file.

newfilename = "new file name"
contents = self.view.substr(sublime.Region(0, self.view.size()))
window = self.view.window()
window.run_command('close_file')
view = window.new_file()
view.set_name(newfilename)
view.settings().set("auto_indent", False)
view.run_command("insert", {"characters": contents})
view.set_scratch(True)
view.run_command("prompt_save_as")

Flow of this sample

  1. Copy all text on current file to memory (contents).
  2. Close current file.
  3. Create new file with new file name.
  4. Paste contents to new file.
  5. Open dialog box for saving new file.

Search Route and Embedding Map using Custom Function on Spreadsheet

This sample script is for searching route between place A and B and embedding a map by custom function on Spreadsheet.

I think that this method is one of various ideas.

Problem

When the map is embedded to a cell on spreadsheet as an image, the function =IMAGE() is suitable for this situation. However, Class Maps, setFormula() for importing =IMAGE() and DriveApp.createFile() for creating images from maps also cannot be used for custom functions.