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

The Thinker

Unicode normalization using Google Apps Script

Overview

This is a script for converting strings from NFD (Normalization Form Decomposition) to NFC (Normalization Form Composition) using Google Apps Script.

Description

Here, I would like to introduce a script for the unicode normalization using Google Apps Script. There are the characters with which is the voiced dot and the characters with which is the semi-voiced dot in Japanese language. When these are used for some applications, there are 2 kinds of usages for the character. For example, when for (\u306f) HA with the voiced dot, there are and ば. These unicodes are \u3070 and \u306f\u3099. Namely, there are the case which displayed 1 character as 2 characters. In most cases, the characters like \u3070 are used. This called NFC (Normalization Form Composition). But we sometimes meet the characters like \u306f\u3099. This called NFD (Normalization Form Decomposition). When the document including such characters which are displayed as 2 characters is converted to PDF file, each character is separated like は ゙. So users often want to convert the characters constructed by 2 characters to the single characters. Recently, String.prototype.normalize was added at ES2015. But ES2015 cannot be used at Google Apps Script yet. And although I had looked for the scripts like this for GAS, unfortunately, I couldn’t find. So I created this script.

Remove ImportError of Module for Sublime Text

Gists

When I launched Sublime Text, I noticed that the error occurred. The error is as follows.

ImportError: No module named 'yaml'

I confirmed that this error occurs when the plugin of Material Theme is read. And the error started to occur after Material Theme was updated, recently.

In this report, I would like to introduce the method for removing this error. The flow is as follows.

  1. Download a file including library for yaml (PyYAML) from https://pypi.python.org/pypi/PyYAML
    • In my environment, I downloaded PyYAML-3.12.win-amd64-py3.5.exe.
  2. Unzip the downloaded file.
    • You can see a directory of yaml.
  3. Add the directory of yaml to python3.3.zip.
    • python3.3.zip is in the directory which installed Sublime Text.

By above flow, the error can be removed. If the error of ImportError occurs for other modules, you can try to do this method. I think that although my Sublime Text is Sublime Text3 build 3143 x64, this method may be able to be used for Sublime Text2.

CLI Tool - gonetatmo

Overview

This is a CLI tool to retrieve data from a personal weather station of Netatmo.

Description

I have a personal weather station of Netatmo. I check the data of my local environment using it. In most case, I have used my browser to retrieve the data so far. About retrieving data using curl, I have created it before. Recently, I thought that I wanted to create this as a CLI too. So I created this. This tool can retrieve not only the data of own Netatmo, but also the data of specific area using Netatmo APIs. By this, I got to be able to retrieve easily the data of various places. This tool has the following features.

Append Values by Inserting Rows using Google Sheets API

Gists

In the case appending values to cell by inserting rows, when sheets.spreadsheets.values.append is used, the values are appended to the next empty row of the last row. If you want to append values to between cells with values by inserting row, you can achieve it using sheets.spreadsheets.batchUpdate.

When you use this, please use your access token.

Endpoint :

POST https://sheets.googleapis.com/v4/spreadsheets/### spreadsheet ID ###:batchUpdate

Request body :

In this request body, it appends the data of “sample1, sample2, sample3” to “A1:A3” of the sheetId of “1234567890”. Before appends the data, it supposes that there are some values at “A1:A3”.

Transposing JSON Object using Google Apps Script

Gists

This is a sample script for transposing JSON object using Google Apps Script.

Input data :

[
    {"key1":"a1","key2":"a2","key3":"a3","key4":"a4","key5":"a5"},
    {"key1":"b1","key2":"b2","key3":"b3","key4":"b4","key5":"b5"},
    {"key1":"c1","key2":"c2","key3":"c3","key4":"c4","key5":"c5"},
    {"key1":"d1","key2":"d2","key3":"d3","key4":"d4","key5":"d5"},
    {"key1":"e1","key2":"e2","key3":"e3","key4":"e4","key5":"e5"}
]

Output data :

{
    "key1": ["a1", "b1", "c1", "d1", "e1"],
    "key2": ["a2", "b2", "c2", "d2", "e2"],
    "key3": ["a3", "b3", "c3", "d3", "e3"],
    "key4": ["a4", "b4", "c4", "d4", "e4"],
    "key5": ["a5", "b5", "c5", "d5", "e5"]
}

Script :

At first, keys have to be defined by yourself, because the order of json is not decided.

var keys = ["key1", "key2", "key3", "key4", "key5"];
var result = {};
data.map(function(_, i){return keys.map(function(f, j){return data[i][keys[j]]})}).forEach(function(e, i){result[keys[i]] = e});
Logger.log(JSON.stringify(result))

Transposing Slice From (n x m) To (m x n) for golang

Gists

This is a sample script for transposing slice from (n x m) to (m x n) for golang.

Script :

package main

import "fmt"

func transpose(slice [][]string) [][]string {
    xl := len(slice[0])
    yl := len(slice)
    result := make([][]string, xl)
    for i := range result {
        result[i] = make([]string, yl)
    }
    for i := 0; i < xl; i++ {
        for j := 0; j < yl; j++ {
            result[i][j] = slice[j][i]
        }
    }
    return result
}

func main() {
    sample := [][]string{
        []string{"a1", "a2", "a3", "a4", "a5"},
        []string{"b1", "b2", "b3", "b4", "b5"},
        []string{"c1", "c2", "c3", "c4", "c5"},
    }
    ar := transpose(sample)
    fmt.Println(ar)
}

Result :

[
    ["a1", "b1", "c1"],
    ["a2", "b2", "c2"],
    ["a3", "b3", "c3"],
    ["a4", "b4", "c4"],
    ["a5", "b5", "c5"]
]

The Go Playground

https://play.golang.org/p/XoZy7m65rEG

Open Site with New Window using Google Apps Script

Gists

This is a sample script for opening a site with new window using Google Apps Script. It is possible to open the site inside the opened dialog box using iframe. But in my situation, I had to open the site as new window. So I created this. As a sample application, it can think of like this. When the special keyword was inputted, open sites and files in Google Drive as a help window. In this case, the trigger installed onEdit() is required to be used. I think that there are some other applications.

Uploading Files From Local To Google Drive by Python without Quickstart

Gists

This is a sample script for uploading files from local PC to Google Drive using Python. In this sample, Quickstart is not used. So when you use this script, please retrieve access token.

Curl sample :

curl -X POST \
    -H "Authorization: Bearer ### access token ###" \
    -F "metadata={name : 'sample.png', parents: ['### folder ID ###']};type=application/json;charset=UTF-8" \
    -F "file=@sample.png;type=image/png" \
    "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"

]

Python sample :

When above curl sample is converted to Python, it becomes as follows.