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

The Thinker

Workaround: Automatically Installing OnEdit Trigger to Copied Google Spreadsheet using Google Apps Script

Gists

This is a workaround for automatically installing the OnEdit trigger to the copied Google Spreadsheet using Google Apps Script.

The sample situation for this workaround is as follows.

  • You have a Google Spreadsheet.
  • Your Spreadsheet is shared with a user as the writer.
  • Your Spreadsheet has a button for executing a script for copying the active Spreadsheet.
  • Your Spreadsheet has a function installedOnEdit for executing by the installable OnEdit trigger.
  • You want to make the user copy the active Spreadsheet by clicking the button, and also, you want to automatically install the OnEdit trigger to installedOnEdit for the copied Spreadsheet, simultaneously.

This method is from “Using OnEdit Trigger to Google Spreadsheet by Hiding Google Apps Script from Other Users (Author: me)” and “Using OnEdit trigger on Google Spreadsheet Created by Service Account (Author: me)”.

Putting TOTP into Google Spreadsheet using Google Apps Script

Gists

In this post, I would like to introduce a sample script for putting Time-based One-time Password (TOTP) value into Google Spreadsheet using Google Apps Script.

In this sample script, I used a Javascript library of https://github.com/hectorm/otpauth . In the current stage, Google Apps Script can run with V8 runtime. By this, it seems that this library can be used with Google Apps Script.

Sample script

function myFunction() {
  const secret = "ABCDEFGHIJKLMN23"; // Please set your secret here.
  const n = 3; // Code is created 5 times every 30 seconds.

  // Loading otpauth.umd.min.js (Ref: https://github.com/hectorm/otpauth)
  const cdnjs =
    "https://cdnjs.cloudflare.com/ajax/libs/otpauth/9.1.1/otpauth.umd.min.js";
  eval(UrlFetchApp.fetch(cdnjs).getContentText());

  const sheet = SpreadsheetApp.getActiveSheet();
  let c = n;
  while (c > 0) {
    const now = new Date();
    const code = new OTPAuth.TOTP({
      secret,
      algorithm: "SHA1",
      digits: 6,
      period: 30,
    }).generate();
    const [start, end] = [now, new Date(now.getTime() + 30000)].map((e) =>
      Utilities.formatDate(e, Session.getScriptTimeZone(), "HH:mm:ss")
    );
    sheet.appendRow([code, `Limit: ${start} - ${end}`]);
    SpreadsheetApp.flush();
    c--;
    if (c > 0) Utilities.sleep(30000);
  }
  sheet.appendRow(["Done."]);
}

Testing

When this script is run, the situation of the above demonstration is obtained.

Enriched Management of Rich Text on Google Spreadsheet using Google Apps Script

Gists

In the current stage, Google Spreadsheet can use rich texts in cells. The rich texts can be also managed by Google Apps Script. But, I thought that creating a script for editing the existing rich text in the cell might be a bit complicated. Because, for example, in the current stage, when the text of the rich text of a cell is changed using a script, all text styles are cleared. In order to add and delete a text for the rich text in a cell, it is required to create a script while the current text style is kept. This is actually complicated. In this post, I would like to introduce the enriched management of rich text on Google Spreadsheet using Google Apps Script. In order to enrich the management of Rich Text using Google Apps Script, I created a library RichTextAssistant.

GAS Library - RichTextAssistant

Overview

This is a GAS library for supporting editing RichText in Google Spreadsheet using Google Apps Script.

Description

There is RichTextApp in my published libraries. RichTextApp can be used mainly for converting RichText to Google Documents and vice versa. This library RichTextAssistant will support editing the rich text in Google Spreadsheets using Google Apps Script. Google Spreadsheet can use rich text as the cell value using Google Apps Script. But, I thought that when I created a script for editing the existing rich text in the cell, it might be a bit complicated. Because, for example, in the current stage, when the text of rich text of a cell is changed using a script, all text styles are cleared. In order to add and delete a text for the rich text in a cell, it is required to create a script while the current text style is kept. This is actually complicated. From this situation, when a script for supporting editing the rich text in a cell is published, it will be useful for a lot of users. So, I created it and published it as “RichTextAssistant” of a Google Apps Script library.

Folder Picker using jsTree with Google Apps Script and Javascript

Gists

This is a sample script for the folder picker using jsTree with Google Apps Script and Javascript.

I have already published “File Picker using Google Apps Script and Javascript without 3rd party”. In this post, jsTree is used.

Usage

1. Install Google Apps Script library.

In this script, “FilesApp” of my Google Apps Script library is used. So, please install it. You can see how to install it at here.

Benchmark: Process Costs for Searching Value using Object with Google Apps Script

Gists

When a value is searched from the 1-dimensional array and a 2-dimensional array, after V8 runtime could be used, I use JSON object, Set object, and Map Object. But, I had never measured the process cost of this situation. In this post, I would like to introduce the process cost for searching a value using a JSON object, Set object, and Map object converted from the 1-dimensional array and 2-dimensional array.

Importing Microsoft Excel to Google Spreadsheet using Custom Function with Google Apps Script

Gists

This is a sample script for importing Microsoft Excel (XLSX) data to Google Spreadsheet using a custom function with Google Apps Script.

Usage

1. Install SheetJS library.

Please copy the script of the SheetJS library from https://cdn.sheetjs.com/xlsx-latest/package/dist/xlsx.full.min.js, and paste the script to the script editor of Google Spreadsheet, and save the script.

In this case, I would like to recommend the following flow.

  1. Add a new script to the script editor. For example, the filename is SheetJS.
  2. Copy and paste the script of https://cdn.sheetjs.com/xlsx-latest/package/dist/xlsx.full.min.js to the added script file, and save the script.
  3. Copy and paste the following sample script of the custom function to the other script file (It’s the default script file (Code.gs)).

2. Prepare custom function.

Please copy and paste the following script to the script editor of Google Spreadsheet (this is the same Spreadsheet installed SheetJS library.) and save the script. And, please reopen Google Spreadsheet.

Directly Retrieving Values from XLSX data using SheetJS with Google Apps Script

Gists

Updated on July 8, 2023

In the current stage, unfortunately, the built-in methods of Google Apps Script cannot directly retrieve the values from the XLSX data. From this situation, I have created DocsServiceApp. When this Google Apps Script library is used, the values are directly retrieved from XLSX data by parsing XML data of XLSX data.

Here, as another approach, I would like to introduce a sample script for directly retrieving the values from XLSX data using SheetJS with Google Apps Script. In the current stage, Google Apps Script can run with V8 runtime. By this, it seems that SheetJS can be used.

Removing Quote Prefix of Cell value using Google Apps Script (Single Quote)

Gists

In Google Spreadsheet, when a single quote is added to the top letter of the cell value, the cell is used as the text value. About detecting this, I have already reported in this post in my blog. In this post, I would like to introduce a sample script for removing the single quote at the top character of the cell value.

Sample script:

function sample() {
  const sheetName = "Sheet1"; // Please set your sheet name.

  const range = SpreadsheetApp.getActiveSpreadsheet()
    .getSheetByName(sheetName)
    .getDataRange();
  range
    .createTextFinder("^'{1,}")
    .useRegularExpression(true)
    .replaceAllWith("");
  range.setValues(range.getValues());
}
  • In this script, for example, '001, '''001, 'abc, and '''abc are converted to 1, 1, abc, and abc, respectively.

Reference: