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

The Thinker

Creating Spreadsheet from Excel file

These scripts can be executed on Script Editor. But, in order to use these, you have to enable Drive API of Advanced Google services and of Google API Console. “Drive API v2” can be used at Google Apps Script by enabling Drive API of Advanced Google services and of Google API Console.

How to use it is as follows.

  1. In the script editor, select Resources > Advanced Google services

  2. In the dialog that appears, click the on/off switch for Drive API v2.

Creating Downloaded Excel file as Spreadsheet

This is a sample GAS script to create an Excel file, which was downloaded from web, as Spreadsheet. By using Drive API, it can be achieved without access token.

Script :

function downloadFile(fileURL, folder) {
  var filename = fileURL.match(".+/(.+?)([\?#;].*)?$")[1];
  var response = UrlFetchApp.fetch(fileURL);
  var rc = response.getResponseCode();
  var blob = response.getBlob();
  var resource = {
    "mimeType": "application/vnd.google-apps.spreadsheet",
    "parents": [{id: folder}],
    "title": filename
  };
  var res = Drive.Files.insert(resource, blob);
  var fileInfo = [rc, res.title, blob.getBytes().length, res.id];
  return fileInfo;
}

Result :

[
    200,
    sample.xlsx,
    10000.0,
    ## file id ##
]

Removes Duplicate JSON Elements for a Value of a Certain Key

This sample removes duplicate JSON elements for a value of a certain key. When the value of the certain key is removed, only a first duplicate element is left. Also I had wanted to be used for Google Apps Script. So it became like this.

Script :

function removeDup(arr, key){
    var temp = [];
    var out = [];
    arr.forEach( function (e, i) {
        temp[i] = (temp.indexOf(e[key]) === -1) ? e[key] : false;
        if (temp[i]) out.push(e);
    });
    return out;
}

JSON :

Flattening Nested Array using CoffeeScript

This sample flattens a nested array using CoffeeScript.

flatten = (array) ->
    array.reduce(((x, y) -> if Array.isArray(y) then x.concat(flatten(y)) else x.concat(y)), [])

console.log flatten [1, [2, 3, [4, 5]], 6, [7, [8, [9], 10] ,11 , 12], 13]

>>> [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 ]

How to use "fields" of Drive APIs

There are a lot of APIs on Google. When we use Google Drive APIs, they usually have “fields” as a resource. The parameter “fields” gives various information which is selected to us. This is one of important parameters. And this can be used at Google Apps Script (GAS) although that version is v2. About how to use it, there are some documents. But it is difficult to find how to use it at GAS. So I would like to write down here as a memorandum. Most parameters for APIs have to be expressed as JSON. However, the expressions are different for each API. I would like to introduce this using some samples. This is for GAS.

Retrieve old revision file from Google Drive

I introduce 2 kinds of methods. One is to use curl. Another is to use wget. At this time, I could know that wget can be also used as same as curl.

In order to use this, at first, please retrieve your access token and enable Drive API.

1. File ID

Retrieve file id from file name.

curl -X GET -sSL \
    -H 'Authorization: Bearer ### Access token ###' \
    'https://www.googleapis.com/drive/v3/files?q=name="### FileName ###"&fields=files(id,name)'
wget -q --header='Authorization: Bearer ### Access token ###' \
    'https://www.googleapis.com/drive/v3/files?q=name="### FileName ###"&fields=files(id,name)' \

Reference : https://developers.google.com/drive/v3/reference/files/list

Put a channel to a channel for golang

I have never heard this. I would like to use this from now.

package main

import "fmt"

type st struct {
    data1 int
    data2 int
}

func main() {
    c1 := make(chan *st, 1)
    c2 := make(chan *st, 1)
    c1 <- &st{1, 2}
    c2 <- <-c1
    close(c1)
    close(c2)
    res, _ := <-c2
    fmt.Println(res.data2)
}

>>> 2

Retrieving File ID from File Name using GAS

This is a sample script of GAS for converting file name to file id on Google Drive. Drive API is used for this. So please enable Drive API at Advanced Drive Services and Google API Console.

function nameToId(filename){
  return [i.id for each (i in Drive.Files.list({q: "title='" + filename + "' and trashed=false"}).items)];
}

Transposing Array From (n x m) To (m x n) for javascript

This script transposes from an array with n rows x m columns to the array with m rows x n columns. In this script, you can use array of n != m .

array = [
    [a1, b1, c1, d1, e1],
    [a2, b2, c2, d2, e2],
    [a3, b3, c3, d3, e3],
    [a4, b4, c4, d4, e4],
    [a5, b5, c5, d5, e5],
    [a6, b6, c6, d6, e6],
    [a7, b7, c7, d7, e7],
    [a8, b8, c8, d8, e8],
    [a9, b9, c9, d9, e9],
    [a10, b10, c10, d10, e10]
]
var result = [];
for (i in array[0]){
    result.push(array.map(function(e,j){return e[i]}));
}
result = [
    [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10],
    [b1, b2, b3, b4, b5, b6, b7, b8, b9, b10],
    [c1, c2, c3, c4, c5, c6, c7, c8, c9, c10],
    [d1, d2, d3, d4, d5, d6, d7, d8, d9, d10],
    [e1, e2, e3, e4, e5, e6, e7, e8, e9, e10]
]

Download Files Without Authorization From Google Drive

Overview

In this article, files can be downloaded without authorization.

Description

When we download files from Google Drive, it usually has to use Drive API. In order to use Drive API, access token is required. If you want to make your friends download files from your Google Drive, the authorization process is to take time. Also Web Link for each files can be used. But it has to set for each files. So I proposal this.