Golang

Updated goodls to v205

  • v2.0.5 (March 10, 2023)

    1. From this version, when the API key is used, the large file is downloaded by the API key. Because the specification for downloading the shared large file is sometimes changed. When the API key is not used, the shared large file is downloaded by the current specification (v2.0.4). If the shared large file cannot be downloaded by the change of the specification on Google’s side, please use the API key and test it again. The usage of goodls has not changed.

The detail information and how to get this are https://github.com/tanaikech/goodls.

Updated goodls to v204

  • v2.0.4 (March 9, 2023)

    1. From January 2024, it seems that the specification of the process for downloading a large shared file on Google Drive has been changed. So I updated goodls to reflect this. The usage of goodls has not changed. In the current stage, when a large share file is downloaded, it is required to click the following button.
    <form
      id="download-form"
      action="https://drive.usercontent.google.com/download"
      method="get"
    >
      <input
        type="submit"
        id="uc-download-link"
        class="goog-inline-block jfk-button jfk-button-action"
        value="このままダウンロード"
      />
      <input type="hidden" name="id" value="fileId" />
      <input type="hidden" name="export" value="download" />
      <input type="hidden" name="authuser" value="0" />
      <input type="hidden" name="confirm" value="t" />
      <input type="hidden" name="uuid" value="uuId" />
      <input type="hidden" name="at" value="some value" />
    </form>
    

    In this version, the URL obtained by this click is created, and the created URL is used for downloading it.

Executing Google Apps Script with Service Account

Gists

Abstract

One day, you might have a situation where it is required to run Google Apps Script using the service account. Unfortunately, in the current stage, Google Apps Script cannot be directly run with the service account because of the current specification. So, this report introduces a workaround for executing Google Apps Script using the service account.

Introduction

When you want to execute Google Apps Script from outside of Google, as the basic approach, it can be achieved by Google Apps Script API. Ref In order to use Google Apps Script, it is required to link the Google Apps Script project with the Google Cloud Platform project. Ref But, in the current stage, Google Apps Script can be executed by Google Apps Script API with only the access token obtained from OAuth2. Unfortunately, the access token obtained by the service account cannot used for executing Google Apps Script using Google Apps Script API. It seems that this is the current specification on the Google side. However, there might be a case that it is required to execute Google Apps Script using the service account. In this report, I would like to introduce a workaround for executing Google Apps Script using the service account. In this workaround, the Web Apps created by Google Apps Script is used. The Web Apps can be used for executing the preserved functions of doGet and doPost from outside of Google. Ref In this workaround, this Web Apps is used for executing the various functions.

Updated: CLI Tool - goris to v301

goris is a CLI tool to search for images with Google Reverse Image Search.

Today, it was updated to v3.0.1. Please check it out. https://github.com/tanaikech/goris

  • v3.0.1 (May 2, 2022)

    1. About the option -w, a bug was resolved. By this, the URLs of the related Web site are returned. As the default, 10 URLs are returned. If you want to retrieve more, please use the option -n like -n 20.

Updated: Go Library - go-getfilelist to v200

go-getfilelist was updated to v2.0.0

  • v2.0.0 (February 25, 2022)

    1. By changing the specification of methods, drive.New() and transport.APIKey were deprecated. By this, I updated the specification of this library as the version 2. When you are required to continue to use drive.New() and transport.APIKey, you can use them with the version 1. At version 1, *http.Client is given to this library. From version 2, *drive.Service is given instead of *http.Client. Please be careful this. You can see the sample script at the above.

You can get this from https://github.com/tanaikech/go-getfilelist

Simple Method for using ggsrun

Gists

This is a simple method for using ggsrun. ggsrun is a CLI tool to execute Google Apps Script (GAS) on a terminal. Also this CLI tool can be used for managing files in Google Drive for OAuth2 and Service Account.

When you use ggsrun, it is required to retrieve the client ID and client secret and/or the service account at Cloud Platform Project. But, there is the case that you want to simply test or you want to just upload and download the files for Google Drive. In this post, I would like to introduce how to simply use ggsrun.

Updated: CLI Tool - gislack

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

  • v1.0.4 (February 27, 2021)

    1. I noticed that this application uses the duplicated methods of Slack API. Ref By this, the duplicated methods were updated to the new methods as follows.

      • From “channels.list” to “conversations.list”.
      • From “channels.history” to “conversations.history”.

Please check it out. https://github.com/tanaikech/gislack

Sample Scripts for Creating New Event with Google Meet Link to Google Calendar using Various Languages

Gists

This is the sample scripts for creating new event with Google Meet link to Google Calendar using various languages. When I saw the official document of “Add video and phone conferences to events”, in the current stage, I can see only the sample script for Javascript. But I saw the several questions related to this for various languages. So I published the sample scripts for creating new event with Google Meet link to Google Calendar using various languages.

Updated: GetFileList for golang, Javascript, Node.js and Python

Updated: GetFileList for golang, Javascript, Node.js and Python

This is the libraries to retrieve the file list with the folder tree from the specific folder of own Google Drive and shared Drives.

Updated: Go Library - go-getfilelist to v103

go-getfilelist was updated to v1.0.3

  • v1.0.3 (May 14, 2020)

    1. Shared drive got to be able to be used. The file list can be retrieved from both your Google Drive and the shared drive.

      • For example, when the folder ID in the shared Drive is used folderID of Folder(folderID), you can retrieve the file list from the folder in the shared Drive.

You can get this from https://github.com/tanaikech/go-getfilelist

Updated ggsrun to v174

  • v1.7.4 (March 11, 2020)

    1. Recently, I noticed that new Google Apps Script project of the standalone script type cannot be created by the create method of Drive API. From now, in order to create the standalone Google Apps Script project, only Google Apps Script API is required to be used. Ref By this, I updated ggsrun. So the command for creating new GAS project is not changed.

      • $ ggsrun u -p ###folderId### -f sample.gs -pn sampleGASProjectName

You can check ggsrun at https://github.com/tanaikech/ggsrun.

Updated goodls to v126

  • v1.2.6 (February 23, 2020)

    1. When the files are downloaded from the shared folder, if an error occurs, the download was stopped. In this thread, a request for skipping this error was proposed. This request was implemented at v1.2.6. Please try the option of --skiperror like below.
      • $ goodls -u https://drive.google.com/drive/folders/###?usp=sharing --skiperror

The detail information and how to get this are https://github.com/tanaikech/goodls.

Updated goodls to v125

  • v1.2.5 (January 29, 2020)

    1. An option for selecting whether the top directory is created was added.
      • $ goodls -u [URL] --notcreatetopdirectory or $ goodls -u [URL] -ntd
      • When this option is NOT used (default situation), when a folder including sub-folders is downloaded, the top folder which is downloaded is created as the top directory under the working directory. When this option is used, the top directory is not created and all files and sub-folders under the top folder are downloaded under the working directory.
      • This feature request was implemented.

The detail information and how to get this are https://github.com/tanaikech/goodls.

Libraries of gdoctableapp for golang, Node.js and python were updated to v110

Libraries of gdoctableapp for golang, Node.js and python were updated to v1.1.0

Libraries of gdoctableapp for golang, Node.js and python were updated to v105

Libraries of gdoctableapp for golang, Node.js and python were updated to v1.0.5

Update History

  • v1.0.5 (January 21, 2020)

    • When the inline objects and tables are put in the table. An error occurred. This bug was removed by this update.
  • I got the pull request at here.

Resumable Uploading Files to Google Drive using Golang

Gists

This is a sample script for the resumable upload of Files to Google Drive using Golang. This script uses the library of google-api-go-client. About the installation of google-api-go-client, please check the Quickstart for golang at the official site.

Sample script:

package main

import (
	"context"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"os"

	drive "google.golang.org/api/drive/v3"

	"golang.org/x/oauth2"
	"golang.org/x/oauth2/google"
	"golang.org/x/oauth2/jwt"
)

// ServiceAccount : Use Service account
func ServiceAccount(credentialFile string) *http.Client {
	b, err := ioutil.ReadFile(credentialFile)
	if err != nil {
		log.Fatal(err)
	}
	var c = struct {
		Email      string `json:"client_email"`
		PrivateKey string `json:"private_key"`
	}{}
	json.Unmarshal(b, &c)
	config := &jwt.Config{
		Email:      c.Email,
		PrivateKey: []byte(c.PrivateKey),
		Scopes: []string{
			drive.DriveScope,
		},
		TokenURL: google.JWTTokenURL,
	}
	client := config.Client(oauth2.NoContext)
	return client
}

func main() {
	filename := "sample.txt"     // Filename
	baseMimeType := "text/plain" // MimeType
	client := ServiceAccount("credential.json") // Please set the json file of Service account.

	srv, err := drive.New(client)
	if err != nil {
		log.Fatalln(err)
	}
	file, err := os.Open(filename)
	if err != nil {
		log.Fatalln(err)
	}
	fileInf, err := file.Stat()
	if err != nil {
		log.Fatalln(err)
	}
	defer file.Close()
	f := &drive.File{Name: filename}
	res, err := srv.Files.
		Create(f).
		ResumableMedia(context.Background(), file, fileInf.Size(), baseMimeType).
		ProgressUpdater(func(now, size int64) { fmt.Printf("%d, %d\r", now, size) }).
		Do()
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Printf("%s\n", res.Id)
}

Note:

  • In this sample, the Service account is used. So the file is uploaded to the Service account’s Drive. When client retrieved from OAuth2 is used, the file is uploaded to owner’s Drive.

References:

Creating a Table to Google Document by Retrieving Values from Google Spreadsheet for Golang

Gists

This is a sample script for creating a table to Google Document by retrieving values from Google Spreadsheet for Golang.

Before you use this script, please install go library of go-gdoctableapp.

$ go get -v -u github.com/tanaikech/go-gdoctableapp

Sample script:

This sample script uses Service Account.

In this sample script, the values are retrieved from Sheet1!A1:C5 of Spreadsheet, and new table is created to the Document using the values.

package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"os"

	gdoctableapp "github.com/tanaikech/go-gdoctableapp"
	"golang.org/x/oauth2"
	"golang.org/x/oauth2/google"
	"golang.org/x/oauth2/jwt"
	docs "google.golang.org/api/docs/v1"
	sheets "google.golang.org/api/sheets/v4"
)

// ServiceAccount : Use Service account
func ServiceAccount(credentialFile string) *http.Client {
	b, err := ioutil.ReadFile(credentialFile)
	if err != nil {
		log.Fatal(err)
	}
	var c = struct {
		Email      string `json:"client_email"`
		PrivateKey string `json:"private_key"`
	}{}
	json.Unmarshal(b, &c)
	config := &jwt.Config{
		Email:      c.Email,
		PrivateKey: []byte(c.PrivateKey),
		Scopes: []string{
			docs.DocumentsScope,
			sheets.SpreadsheetsScope,
		},
		TokenURL: google.JWTTokenURL,
	}
	client := config.Client(oauth2.NoContext)
	return client
}

func main() {
	spreadsheetID := "###" // Please set here
	documentID := "###"    // Please set here

	client := ServiceAccount("credential.json") // Please set the json file of Service account.
	srv, err := sheets.New(client)
	if err != nil {
		log.Fatalf("%v", err)
	}

	// Retrieve values from Spreadsheet.
	sheetValues, err := srv.Spreadsheets.Values.Get(spreadsheetID, "Sheet1!A1:C5").Do()
	if err != nil {
		log.Fatalf("%v", err)
	}
	values := sheetValues.Values

	// Put the retrieved values to Document.
	g := gdoctableapp.New()
	obj := &gdoctableapp.CreateTableRequest{
		Rows:    int64(len(values)),
		Columns: int64(len(values[0])),
		Append:  true,
		Values:  values,
	}
	res, err := g.Docs(documentID).CreateTable(obj).Do(client)
	if err != nil {
		log.Fatalf("%v", err)
	}
	fmt.Println(res)
}

References:

Go Library - go-gdoctableapp

Overview

This is a Golang library for managing tables on Google Document using Google Docs API.

Description

Google Docs API has been released. When I used this API, I found that it is very difficult for me to manage the tables on Google Document using Google Docs API. Although I checked the official document, unfortunately, I thought that it’s very difficult for me. So in order to easily manage the tables on Google Document, I created this library.

Dynamically Retrieving Keys and Values from Struct Property

Gists

This is a sample script for dynamically retrieving the keys and values from struct property using golang.

Sample script:

Go Playground

package main

import (
	"fmt"
	"reflect"
)

func main() {
	s := struct {
		key1 string
		key2 string
		key3 string
	}{"value1", "value2", "value3"}

	r := reflect.ValueOf(&s).Elem()
	rt := r.Type()
	for i := 0; i < rt.NumField(); i++ {
		field := rt.Field(i)
		rv := reflect.ValueOf(&s)
		value := reflect.Indirect(rv).FieldByName(field.Name)
		fmt.Println(field.Name, value.String())
	}
}

Sorting for Slice using Golang

Gists

This is a sample script for sorting a slice using Golang. Recently, I had a situation for sorting the coordinates of cells of Spreadsheet. As a test case, it thinks of the situation that the randomized cells are sorted. I think that this can be also used for a table except for Spreadsheet.

Sample slice:

The sample slice is as follows.

ar := []struct {
    row   int
    col   int
    value string
}{
    {0, 0, "A1"},
    {0, 1, "B1"},
    {0, 2, "C1"},
    {1, 0, "A2"},
    {1, 1, "B2"},
    {1, 3, "D2"},
    {2, 0, "A3"},
    {2, 2, "C3"},
}

When each element of above slice is put to a Spreadsheet, it becomes as follows.

Go Library - go-fetchall

Overview

This is a Golang library for running HTTP requests with the asynchronous process. The progress of requests can be also shown.

Demo

In this demonstration, 5 requests are run by 2 workers. And before each request, the waiting time for 2 seconds is added as a sample. By this, you can easily see the work with 2 workers. Also you can see this script at the following sample script.

Retrieving Access Token using Service Account by Google's OAuth2 package for Golang

Gists

This is a sample golang script for retrieving access token using Service Account of Google by Google’s OAuth2 package.

The script without using Google’s OAuth2 package is here.

package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "os"

    "golang.org/x/oauth2"
    "golang.org/x/oauth2/google"
    "golang.org/x/oauth2/jwt"
)

func serviceAccount(credentialFile string) (*oauth2.Token, error) {
    b, err := ioutil.ReadFile(credentialFile)
    if err != nil {
        return nil, err
    }
    var c = struct {
        Email      string `json:"client_email"`
        PrivateKey string `json:"private_key"`
    }{}
    json.Unmarshal(b, &c)
    config := &jwt.Config{
        Email:      c.Email,
        PrivateKey: []byte(c.PrivateKey),
        Scopes: []string{
            "https://www.googleapis.com/auth/drive.metadata.readonly",
        },
        TokenURL: google.JWTTokenURL,
    }
    token, err := config.TokenSource(oauth2.NoContext).Token()
    if err != nil {
        return nil, err
    }
    return token, nil
}

func main() {
    token, err := serviceAccount("credentials.json") // Please set here
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    fmt.Println(res)
}

Updated ggsrun to v160

ggsrun was updated to v.1.6.0

  • v1.6.0 (November 30, 2018)
    1. Although at ggsrun, files can be searched by filename and file ID, searching files using search query and regex couldn’t be done. From version 1.6.0, files got to be able to be searched using the search query and regex.
      • $ ggsrun sf -q "### search query ###" -f "### fields ###" -r "### regex ###"
    2. Some modifications.

You can check ggsrun at https://github.com/tanaikech/ggsrun.

Using String Values to []googleapi.Field for Golang

Gists

This sample script is for using the string values to []googleapi.Field for Golang. The property of fields can often be used to the Google APIs. When such APIs are used by the Go library, there are the cases that fields parameter is required to be used. For example, at the quickstart of Drive API for golang, the value is directly put to Fields() like r, err := srv.Files.List().PageSize(10).Fields("nextPageToken, files(id, name)").Do(). For this situation, when the string value is put to Fields() as follows,

Updated: Go Library - go-getfilelist to v101

go-getfilelist was updated to v1.0.1

  • v1.0.1 (November 13, 2018)

    1. From this version, in order to retrieve files and file information, “google.golang.org/api/drive/v3” is used.
      • By this, when the values are retrieved from this library, users can use the structure of drive.File.
      • Script using this library can be seen at goodls.

You can get this from https://github.com/tanaikech/go-getfilelist

Updated ggsrun to v152

ggsrun was updated to v.1.5.2

  • v1.5.2 (November 4, 2018)
    1. About downloading folders, when files are downloaded from a folder, you can download Google Docs files with the mimeType you want. For example, when you download files from the folder, if -e txt is used, Google Docs are downloaded as the text file. When -e pdf is used, they are downloaded as the PDF file. Of course, there are mimeType which cannot be converted.
      • $ ggsrun d -f [folderName] -e txt -j
    2. About uploading files, when files are uploaded from your local PC, the files got to be able to be converted to Google Docs. For this, new option of --convertto, -c is added. For example, when a text file is uploaded, if you use -c doc, the text file is uploaded as Google Document.
      • $ ggsrun u -f [fileName] -c doc -j

You can check ggsrun at https://github.com/tanaikech/ggsrun.

Updated goodls to v110

goodls was updated to v.1.1.0

  • v1.1.0 (November 4, 2018)

    1. By using API key, files from the shared folder got to be able to be downloaded while keeping the folder structure.
      • This demonstration can be seen at Demo.
    2. By using API key, the information of shared file and folder can be also retrieved.
    3. About the option of --extension and -e, when -e ms is used, Google Docs (Document, Spreadsheet, Slides) are converted to Microsoft Docs (Word, Excel, Powerpoint), respectively.

The detail information and how to get this are https://github.com/tanaikech/goodls.

Go Library - go-getfilelist

Overview

This is a Golang library to retrieve the file list with the folder tree from the specific folder of Google Drive.

Description

When I create applications for using Google Drive, I often retrieve a file list from a folder in the application. So far, I had created the script for retrieving a file list from a folder for each application. Recently, I thought that if there is the script for retrieving the file list with the folder tree from the folder of Google Drive as a library, it will be useful for me and other users. So I created this.

Updated ggsrun to v150

ggsrun was updated to v.1.5.0

  • v1.5.0 (October 27, 2018)
    1. From this version, ggsrun got to be able to download all files and folders in the specific folder in Google Drive. When all files are downloaded from a folder, the same folder structure of Google Drive is created to the local PC.
      • $ ggsrun d -f folderName or folderId
        • When the project file is downloaded, it is downloaded as a zip file. All scripts in the project is put in the zip file.
        • Also when you download a single project, you can use an option --zip or -z. By this, the downloaded project is saved as a zip file.
        • This new function can be also used for the shared folders. When you want to download the files from the shared folder, please use the folder ID of the shared folder.
    2. The file list with the folder tree in the specific folder got to be able to be retrieved.
    3. When the files are downloaded, the progression got to be able to be seen. When you want to see the progression, please use -j when you download files and folders.
    4. Files with large size got to be able to be used. In order to download files with large size (several gigabytes), files are saved by chunks.
    5. Some modifications.

You can check ggsrun at https://github.com/tanaikech/ggsrun.

Zip Compression of Downloaded File using Golang

Gists

This is a sample script for creating a downloaded file as a zip file using Golang. The downloaded file is not created to a file as a temporal file. The zip file is directly created. When you use this, please modify url, downloadedFileName and zipFileName.

Sample script:

package main

import (
    "archive/zip"
    "bytes"
    "fmt"
    "io"
    "io/ioutil"
    "log"
    "net/http"
    "os"
    "time"
)

func main() {
    url := "https://localhost/sample.png"
    downloadedFileName := "sample.png"
    zipFileName := "sample.zip"

    res, err := http.Get(url)
    if err != nil {
        log.Fatal(err)
    }
    body, err := ioutil.ReadAll(res.Body)
    if err != nil {
        fmt.Fprintf(os.Stderr, "Error: %v. ", err)
        os.Exit(1)
    }
    defer res.Body.Close()
    buf := new(bytes.Buffer)
    w := zip.NewWriter(buf)
    fh := &zip.FileHeader{
        Name:     downloadedFileName,
        Modified: time.Now(),
        Method:   8,
    }
    f, err := w.CreateHeader(fh)
    if err != nil {
        log.Fatal(err)
    }
    if _, err := f.Write(body); err != nil {
        log.Fatal(err)
    }
    err = w.Close()
    if err != nil {
        log.Fatal(err)
    }
    file, err := os.Create(zipFileName)
    if err != nil {
        log.Fatal(err)
    }
    if _, err = io.Copy(file, buf); err != nil {
        log.Fatal(err)
    }
    file.Close()
    fmt.Println("Done.")
}

Note:

As an important point, when the file is downloaded, os.FileInfo cannot be used. So in this situation, it uses zip.FileHeader. At that time, please remember to set Method. Method is 0 as the default. This means no compression. The sample script uses 8 to Method. This means the DEFLATE method.

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.

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

Updated ggsrun to v141

ggsrun was updated to v.1.4.1

  • v1.4.1 (February 9, 2018)
    1. For uploading, the resumable-upload method was added.
      • The resumable-upload method is automatically used by the size of file.
        • “multipart/form-data” can upload files with the size less than 5 MB.
        • “resumable-upload” can upload files with the size more than 5 MB.
      • The chunk for resumable-upload is 100 MB as the default.
        • Users can also give this chunk size using an option.
      • $ ggsrun u -f filename -chunk 10
        • This means that a file with filename is uploaded by each chunk of 10 MB.

You can read “How to install” at here.

Copying Values from JSON to Struct using reflect Package

Gists

This is a sample script for copying values from JSON to a struct using reflect package.

Script :

package main

import (
	"encoding/json"
	"fmt"
	"reflect"
)

type obj struct {
	Key1 string `json:"k1"`
	Key2 string `json:"k2"`
	Key3 int64  `json:"k3"`
	Key4 int    `json:"k4"`
	Key5 bool   `json:"k5"`
}

func main() {
	data := `{"k1": "v1", "k2": "v2", "k3": 1234567890, "k4": 456, "k5": true}`
	d := map[string]interface{}{}
	json.Unmarshal([]byte(data), &d)
	obj := &obj{}
	s := reflect.ValueOf(obj).Elem()
	typeOfT := s.Type()
	for i := 0; i < s.NumField(); i++ {
		for j, f := range d {
			if typeOfT.Field(i).Tag.Get("json") == j {
				fl := s.FieldByName(typeOfT.Field(i).Name)
				switch fl.Kind() {
				case reflect.Bool:
					fl.SetBool(f.(bool))
				case reflect.Int, reflect.Int64:
					c, _ := f.(float64)
					fl.SetInt(int64(c))
				case reflect.String:
					fl.SetString(f.(string))
				}
			}
		}
	}
	fmt.Printf("%+v\n", obj) // &{Key1:v1 Key2:v2 Key3:1234567890 Key4:456 Key5:true}
}

Result :

&{Key1:v1 Key2:v2 Key3:1234567890 Key4:456 Key5:true}

The Go Playground

https://play.golang.org/p/Rz-GNbFyDfh

Parsing JSON object (keys are number and changing every time)

Gists

This sample script is for parsing JSON object. In the object, the keys are number and changing every time.

Object:

{
    "key1": {
        "key2": [
            {"0": [{"key3": "value3a"}, {"key3": "value3b"}]},
            {"1": [{"key3": "value3c"}, {"key3": "value3d"}]}
        ]
    }
}

Script:

package main

import (
    "encoding/json"
    "fmt"
    "strconv"
)

type key1 struct {
    Key1 key2 `json:"key1"`
}

type key2 struct {
    Key2 []interface{} `json:"key2"`
}

func main() {
    data := `{"key1": {"key2": [{"0": [{"key3": "value3a"}, {"key3": "value3b"}]},{"1": [{"key3": "value3c"}, {"key3": "value3d"}]}]}}`
    k1 := &key1{}
    json.Unmarshal([]byte(data), &k1)
    for i, e := range k1.Key1.Key2 {
        if key, ok := e.(map[string]interface{})[strconv.Itoa(i)].([]interface{}); ok {
            for _, f := range key {
                fmt.Printf("%+v\n", f.(map[string]interface{})["key3"])
            }
        }
    }
}

Result:

value3a
value3b
value3c
value3d

The Go Playground

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

Updated ggsrun to v140

ggsrun was updated to v.1.4.0

You can read “How to install” at here.

CLI Tool - goodls

Overview

This is a CLI tool to download shared files from Google Drive.

Description

We have already known that the shared files on Google Drive can be downloaded without the authorization. But when the size of file becomes large (about 40MB), it requires a little ingenuity to download the file. It requires to access 2 times to Google Drive. At 1st access, it retrieves a cookie and a code for downloading. At 2nd access, the file is downloaded using the cookie and code. I created this process as a CLI tool. This tool has the following features.

Updated ggsrun to v134

ggsrun was updated to v.1.3.4

  • v1.3.4 (January 2, 2018)

    1. Added new option for downloading ‘bound-scripts’ of Google Sheets, Docs, or Forms file.
      • When the bound-scripts are downloaded, the project name cannot be retrieved because Drive API cannot be used for the bound-scripts. So when the bound-scripts are downloaded, the project ID had been used previously. Such filename is not easily to be seen. By this additional option, users can give the filename when it downloads the bound-scripts.
      • The usage is here
    2. Removed a bug.
      • When a project is downloaded, script ID in the project is added to the top of each downloaded script as a comment. There was a problem at the character using for the comment out. This was modified.

You can check this and download ggsrun at https://github.com/tanaikech/ggsrun.

Create New Project with Original Manifests

It was found that you can also create new project with your original Manifests using ggsrun. By using this, for example, when you created new project, the project can have libraries, Advanced Google Services and so on at the initial stage. I think that this can be used as a template for Project.

$ ggsrun u -pn [Project name] -f appsscript.json

https://github.com/tanaikech/ggsrun/blob/master/help/README.md#ModifyManifests

You can check this and download ggsrun at https://github.com/tanaikech/ggsrun.

Updated ggsrun to v133

ggsrun was updated to v.1.3.3

Awesome points of Manifests :

Awesome points of Manifests that I think are below.

Updated ggsrun to v132

ggsrun was updated to v.1.3.2

  • v1.3.2 (October 20, 2017)

    1. Updated ggsrun’s Install manual (README.md). Since I thought that the manual became too complicated, I separated it to the simple version and the detail version. And also , recently, since Google’s specification was updated, about how to deploy API executable and enable APIs for ggsrun’s Install manual were updated.
    2. From this version, scripts in a project can be rearranged. The rearrangement can be done by interactively on your terminal and/or a configuration file. The usage is here
      • For rearranging scripts, there is one important point. When scripts in a project is rearranged, version history of scripts is reset once. So if you don’t want to reset the version history, before rearranging, please copy the project. By copying project, the project before rearranging is saved.

Updated go-rearrange and gorearrange to v102

go-rearrange and gorearrange were updated to v.1.0.2

From this version, data included multi-bytes characters can be used. At Linux, it works fine. At Windows DOS, rearranging and selecting data can be done. But the displayed data is shifted. Although this may be a bug of termbox-go, I don’t know the reason. I’m sorry. On the other hand, data with only single-byte characters works fine. About MAC, I don’t have it. If someone can confirm and tell me it, I’m glad.

Updated go-rearrange and gorearrange to v101

go-rearrange and gorearrange were updated to v.1.0.1

  • As one of outputs, indexmode (bool) was added. If this is true, the rearranged result is output as the change of index for the source data. For example, if the source data and rearranged data are ["a", "b", "c"] and ["c", "b", "a"], respectively. The output will become [2, 1, 0].

By this, the specification for creating applications will expand.

The detail information and how to get this are https://github.com/tanaikech/gorearrange.

Uploading CSV File as Spreadsheet and Modifying Permissions using Golang

Gists

This sample script is for uploading CSV file as Spreadsheet and modifying permissions using Golang.

I think that the detail information of google-api-go-client is a bit little. The sample scripts are so little. It retrieves most information from only godoc and GitHub. So I publish such sample scripts here. If this is useful for you, I’m glad.

Important points :

  1. Give mimeType of file that it wants to upload to options of Media(r io.Reader, options ...googleapi.MediaOption).
  2. In order to give options, use googleapi.ContentType().
  3. Give mimeType of file that it wants to convert, when it uploads it to Google Drive, to file of Create(file *File).
  4. In order to give file, use &drive.File{}.
  5. For installing permissions, use &drive.Permission{}. Each parameter is the same to them for Python.

This sample script uses Quickstart. So in order to use this sample script, at first, please do Step 1 and Step 2 of the Quickstart.

spreadsheets.values.batchUpdate using Golang

Gists

Flow :

In my sample script, the script was made using the Quickstart. The flow to use this sample script is as follows.

  1. For Go Quickstart, please do Step 1 and Step 2.
  2. Please put client_secret.json to the same directory with my sample script.
  3. Copy and paste my sample script, and create it as new script file.
  4. Run the script.
  5. When Go to the following link in your browser then type the authorization code: is shown on your terminal, please copy the URL and paste to your browser. And then, please authorize and get code.
  6. Put the code to the terminal.
  7. When Done. is displayed, it means that the update of spreadsheet is done.

Request body :

For Spreadsheets.Values.BatchUpdate, BatchUpdateValuesRequest is required as one of parameters. In this case, the range, values and so on that you want to update are included in BatchUpdateValuesRequest. The detail information of this BatchUpdateValuesRequest can be seen at godoc. When it sees BatchUpdateValuesRequest, Data []*ValueRange can be seen. Here, please be carefull that Data is []*ValueRange. Also ValueRange can be seen at godoc. You can see MajorDimension, Range and Values in ValueRange.

Updated ggsrun to v131

ggsrun was updated to v.1.3.1

  1. Recently, when scripts on local PC is uploaded to Google Drive as a new project, the time to create on Google became a bit long. (I think that this is due to Google Update.) Under this situation, when the script is uploaded, the timeout error occurs while the new project is created using the script. So the time until timeout of fetch was modified from 10 seconds to 30 seconds. By this, when the script is uploaded, no error occurs and the information of the created project is shown.
    • You can create a new project on Google Drive using scripts on local PC. The sample command is ggsrun u -f sample.gs1,sample2.gs,sample3.html -pn newprojectname

You can check this and download ggsrun at https://github.com/tanaikech/ggsrun.

Updated ggsrun to v130

ggsrun was updated to v.1.3.0

  1. From this version, container-bound scripts can be downloaded. The container-bound script is the script created at the script editor on Google Sheets, Docs, or Forms file.
    • In order to download container-bound scripts, the project ID of container-bound scripts is required. The project ID can be retrieved as follows.
      • Open the project. And please operate follows using click.
        • -> File
        • -> Project properties
        • -> Get Script ID (This is the project ID.)
  2. When a project is downloaded, the filename of HTML file had become .gs. This bug was modified.

The usage is here.

Exporting Project on Google Drive using Golang Quickstart

Gists

This is a sample script for exporting a project on Google Drive to local PC using Golang Quickstart. A file with refresh token is saved to the same directory with this script as go-quickstart.json. Before you run this script, please enable Drive API on your Google API console.

Points for exporting project

  1. In order to export project, both drive.DriveScriptsScope and drive.DriveScope have to be included in the scope.
  2. The mimeType for exporting has to be “application/vnd.google-apps.script+json”.

If you already have the file with refresh token, at first, please delete it and run this script. By this, the scopes of refresh token and access token are updated.

Updated ggsrun to v122

ggsrun was updated to v.1.2.2

  1. For Google Docs (spreadsheet, document, slide and drawing), since I noticed that the revision files would not be able to be retrieved using Drive API v3, I modified this using new workaround.
    • The new workaround is to use Drive API v2. drive.revisions.get of Drive API v2 can retrieve not only the revision list, but also the export links. I thought of the use of the export links. This became the new workaround.
    • For the files except for Google Docs, the revision files can be retrieved using Drive API v3.
    • The usage is here.

I don’t know when this workaround will not be able to be used. But if this could not be used, I would like to investigate of other method.

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.

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.

Updated: CLI Tool - goris

goris is a CLI tool to search for images with Google Reverse Image Search.

Today, it was updated to v1.1.0. Please check it out. https://github.com/tanaikech/goris

When images are matched to a searched image, web pages with matching images are retrieved. These are web pages displayed on Google top page. When this is not used, images are retrieved. This was added as a boolean option. (This was added by a request.)

Slice Created by Split at Golang

When a string without no strings is split by strings.Split(), the created slice is the same to the slice created by make(). The length of the slice doesn’t become zero.

Sample script :

package main

import (
    "fmt"
    "strings"
)

func main() {
    sample1a := strings.Split("", " ")
    fmt.Printf("%v, %v, '%v', %v, %+q\n", sample1a, len(sample1a), sample1a[0], len(sample1a[0]), sample1a[0])

    sample1b := make([]string, 1)
    fmt.Printf("%v, %v, '%v', %v, %+q\n", sample1b, len(sample1b), sample1b[0], len(sample1b[0]), sample1b[0])

    var sample2a []string
    fmt.Printf("%v, %v\n", sample2a, len(sample2a))

    sample2b := []string{}
    fmt.Printf("%v, %v\n", sample2b, len(sample2b))
}

Result :

strings.Split() : [], 1, '', 0, ""
make()          : [], 1, '', 0, ""
var                : [], 0
[]string{}      : [], 0

Changing from 'float64' to 'int' for Values did Unmarshal using 'map[string]interface{}'

This sample is for changing from “float64” to “int” for values did unmarshal using map[string]interface{}.

When it did unmarshal using map[string]interface{}, a number with “int” was changed to “float64”. And it shows an error as follows.

Error :

panic: interface conversion: interface {} is float64, not int

Sample Script : It solves using following script.

package main

import (
    "encoding/json"
    "fmt"
    "reflect"
)

func main() {
    data := `{"key": 10}`
    var i map[string]interface{}
    json.Unmarshal([]byte(data), &i)

    val1 := i["key"]
    fmt.Printf("%v, %v\n", val1, reflect.TypeOf(val1)) // 10, float64

    i["key"] = int(i["key"].(float64))
    val2 := i["key"]
    fmt.Printf("%v, %v\n", val2, reflect.TypeOf(val2)) // 10, int
}

Go Playground

Replacing JSON Key by Golang

This sample is for replacing JSON key by golang.

package main

import (
    "encoding/json"
    "fmt"
)

func main() {
    json1 := `{"key1": "value1"}`

    obj := map[string]interface{}{}
    json.Unmarshal([]byte(json1), &obj)

    fmt.Println(obj) // <-- map[key1:value1]

    obj["key2"] = obj["key1"]
    delete(obj, "key1")

    fmt.Println(obj) // <-- map[key2:value1]
}

Benchmark: Splitting Command-Line Arguments by Golang

This sample script is for splitting command-line arguments by golang. There are 2 types. One is the regular expression is used. Another is that Split() and TrimSpace() are used.

Here, each process speed was compared.

Script :

package main

import (
    "regexp"
    "strings"
    "testing"
)

func BenchmarkB1(b *testing.B) {
    str := "test1.txt, test2.txt"
    b.ResetTimer()
    for i := 0; i < b.N; i++ {
        ar := regexp.MustCompile(`\s*,\s*`).Split(str, -1)
        var result []string
        for _, x := range ar {
            result = append(result, x) // --> 'test.js', 'test2.py'
        }
        _ = result
    }
}

func BenchmarkB2(b *testing.B) {
    str := "test1.txt, test2.txt"
    b.ResetTimer()
    for i := 0; i < b.N; i++ {
        ar := strings.Split(str, ",")
        var result []string
        for _, x := range ar {
            result = append(result, strings.TrimSpace(x)) // --> 'test.js', 'test2.py'
        }
        _ = result
    }
}

Result :

$ go test -bench .
BenchmarkB1-4             100000             13048 ns/op
BenchmarkB2-4            3000000               399 ns/op
PASS

Just as expected, the regular expression was slow. And it’s much slower than that of Split() and TrimSpace().

Decoding JSON by Golang

Decoding JSON by Golang

func main() {
    data := `{
      "A_key1": {
        "B_key1": {
          "C_key": "value"
        }
      },
      "A_key2": {
        "B_key2": {
          "C_key": "value"
        }
      },
      "A_key3": {
        "B_key3": {
          "C_key": "value"
        }
      },
      "A_key4": {
        "B_key4": {
          "C_key": "value"
        }
      },
      "A_key5": {
        "B_key5": {
          "C_key": "value"
        }
      }
    }`
    var p interface{}
    json.NewDecoder(strings.NewReader(data)).Decode(&p)
    fmt.Println(p)
}

Go Playground

Dynamical Nested JSON Objects by Golang

This sample script dynamically creates nested JSON objects.

Script

obj := map[string]interface{}{}
for i := 1; i <= 5; i++ {
    value := map[string]interface{}{
        fmt.Sprintf("B_key%d", i): map[string]interface{}{
            "C_key": "value",
        },
    }
    obj[fmt.Sprintf("A_key%d", i)] = value
}

Result

{
  "A_key1": {
    "B_key1": {
      "C_key": "value"
    }
  },
  "A_key2": {
    "B_key2": {
      "C_key": "value"
    }
  },
  "A_key3": {
    "B_key3": {
      "C_key": "value"
    }
  },
  "A_key4": {
    "B_key4": {
      "C_key": "value"
    }
  },
  "A_key5": {
    "B_key5": {
      "C_key": "value"
    }
  }
}

Updated: CLI Tool - gogauth

gogauth is a CLI tool to easily retrieve access token for using APIs on Google.

I thought that if the access token can easily retrieve, it may be useful for users who can use various convenience Google APIs. So I created this. Also this can be used for testing sample script like the demo. If this will be helpful for you, I’m glad.

Today, it was updated to v2.0.1. Please check it out. https://github.com/tanaikech/gogauth

CLI Tool - goris

Overview

This is a CLI tool to search for images with Google Reverse Image Search.

Motivation

Because I had wanted to search for images with an image URL and file on my terminal, I created this. This can download images from searched image URLs.

The detail information and how to get this are https://github.com/tanaikech/goris.

CLI Tool - ggsrun

Overview

This is a CLI tool to execute Google Apps Script (GAS) on a terminal.

Motivation

Will you want to develop GAS using CoffeeScript on your local PC? Generally, when we develop GAS, we have to login to Google using own browser and develop it using Javascript on the Script Editor. Recently, I have wanted to have more convenient local-environment for developing GAS. So I created this “ggsrun”.

The detail information and how to get this are https://github.com/tanaikech/ggsrun.

CLI Tool - gogauth

Released a CLI tool for easily retrieving accesstoken from Google OAuth2. The title is gogauth. When I have seen stackoverflow, I knew that it is difficult to retrieve accesstoken from Google OAuth2. So I made this.

Features of this CLI tool are as follows.

  1. Retrieves easily accesstoken from Google OAuth2 for using Drive API.

  2. If you have PhantomeJS, this retrieves “code” from Google using it. So you don’t need to launch your browser for retrieving “code”.