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

The Thinker

Update JSON object using jq

Gists

Achieving the following conversion using jq. In this conversion, when key2 is true, updated_ is added to the value of key1 and the value of key2 is 2 times and key2a is added by adding updated_ to the value as new property.

Conversion

From

[
  {
    "key1": "value1",
    "key2": 123,
    "key3": true
  },
  {
    "key1": "value2",
    "key2": 456,
    "key3": false
  },
  {
    "key1": "value3",
    "key2": 789,
    "key3": true
  }
]

To

[
  {
    "key1": "updated_value1",
    "key2": 246,
    "key2a": "updated_123",
    "key3": true
  },
  {
    "key1": "value2",
    "key2": 456,
    "key3": false
  },
  {
    "key1": "updated_value3",
    "key2": 1578,
    "key2a": "updated_789",
    "key3": true
  }
]

Command

$ echo '[{"key1":"value1","key2":123,"key3":true},{"key1":"value2","key2":456,"key3":false},{"key1":"value3","key2":789,"key3":true}]' | jq '[.[] | select(.key3 == true) |= {"key1": ("updated_" + .key1), "key2": (.key2 * 2), "key2a": ("updated_" + (.key2|tostring)), "key3": .key3}]'

Testing

https://jqplay.org/s/RbKiioures

Search Dialog Sample using TextFinder with Google Apps Script

Gists

This is a sample script for the search dialog using TextFinder with Google Apps Script. If this sample script could help to indicate the possibility of TextFinder, I’m glad.

Demo

In this demonstration, the value of test is searched. When "NEXT" is clicked, the next searched value is activated. When "PREVIOUS" is clicked, the previous searched value is activated. The search can be done for all sheets in the Google Spreadsheet.

Workaround: Putting Multiple Hyperlinks to a Cell using Sheets API

Gists

This is a current workaround for putting the multiple hyperlinks to a cell using Sheets API.

Description

Recently, at Spreadsheet service, the multiple hyperlinks got to be able to be put to a cell. Ref In this case, it can be achieved using RichTextValue. On the other hand, at Sheets API, in the current stage, there are no methods for directly putting the multiple hyperlinks to a cell. And also, such methods have not been added. I believe that such methods will be added in the future update. I think that when this is implemented, it might be added to TextFormatRun.

Managing Texts on Google Slides using Google Apps Script

Gists

This is a sample script for managing the texts on Google Slides using Google Apps Script. Recently, I got the request like this. I published this here, because I thought that this might be also useful for other users.

Demo

In this demonstration, the text of {{baz}} on Google Slides are searched and replaced to other text, and also, the text style is changed.

Sample situation

In this case, it supposes that there are 3 types of shapes in the slide. Those are the text box, the rectangle shape and the explosion shape. Each shape has the texts and the title of the shape like shape1, shape2 and shape3. The sample script searches the shape using the title. You can see it at the following image.

Workaround: Correctly Exporting Charts on Google Spreadsheet as Images using Google Apps Script

Gists

This is a sample script for correctly exporting the charts on Google Spreadsheet as the images using Google Apps Script. In the current stage, using Google Apps Script, when the charts on Google Spreadsheet are exported as the images, it seems that the exported images are not the same with the original one on Google Spreadsheet. About this, today, I could notice that I had answered for 2 questions. Q1, Q2 And also, I had already been reported this at the issue tracker. Ref

Retrieving Difference Between 2 Arrays using Google Apps Script

Gists

This is a sample script for retrieving the difference between 2 arrays, which are the old values and the new values, using Google Apps Script. In my environment, I sometimes have the situation that it is required to retrieve the difference between 2 arrays. So I prepared this as a sample script. I think that this can be also used at Javascript and Node.js. If this was also useful for your situation, I’m glad.

Updated: GAS Library - RichTextApp

RichTextApp was updated to v1.1.2

  • v1.1.0 (June 16, 2020)

    1. Add new method of RichTextToHTMLForSpreadsheet. The method of RichTextToHTMLForSpreadsheet can convert the rich texts in the cells to the HTML format.
  • v1.1.1 (June 16, 2020)

    1. About the method of RichTextToHTMLForSpreadsheet, I forgot to convert hyperlinks to HTML. This was modified.
  • v1.1.2 (June 16, 2020)

    1. When one row and several columns are used as the range, only 1st column is returned. This bug was removed.

You can see the detail information here https://github.com/tanaikech/RichTextApp

Transfer of owner of files got to be able to be achieved with batch requests of Drive API

Today, I could confirm that the transfer of owner of files got to be able to be achieved with batch requests of Drive API. When I had tested this at January 31, 2020, an error of there is no function to change the owner of this item yet (currently under development) had occurred.

But today, I could confirm that this got to be able to be achieved by the batch requests. By this, the owner of a lot of files can be transferred by reducing both the process cost and the quota cost.

Batch Requests for Drive API using Google Apps Script

Overview

These are the sample scripts of the batch requests for Drive API using Google Apps Script.

Description

When we want to manage the files and folders on Google Drive, we have 2 ways. One is the use of Drive service. Another is the use of Drive API. In the case of them, when we want to manage a lot of files and folders, unfortunately, both ways have no batch requests. Namely, for example, the metadata of a lot of files are changed, the methods in Class File and Files: update are required to be used in the loop. In this case, the process cost will be high. On the other hand, Drive API can use the batch requests. But in this case, in order to use this batch requests with Google Apps Script, it is required to create the request body of multipart/mixed by each user. Because there are no methods for automatically requests the batch requests. From this situation, here, I would like to introduce the simple sample scripts for creating, updating and deleting the files and folders on Google Drive using the batch requests with Google Apps Script.