Splitting and Processing an Array every n length using Google Apps Script

Gists

This is a sample script for splitting and processing an array every n length using Google Apps Script. When I prepare a sample script with Google Apps Script, I sometimes have the situation that it is required to split and process an array every n length. This sample script is for achieving this situation.

Please set limit. This sample script splits the sample array every 3 length.

When you use this script with Google Apps Script, please enable V8 runtime.

Report: Obtaining Values from GOOGLEFINANCE using Google Apps Script

Gists

This is a report for obtaining the values from GOOGLEFINANCE using Google Apps Script. When I tested to retrieve the values from GOOGLEFINANCE function on Google Spreadsheet using Google Apps Script, I noticed that the values can be retrieved.

When I had tested this before, I had got the value of #N/A. About retrieving the values from GOOGLEFINANCE function on Google Spreadsheet, I had known “Historical GOOGLEFINANCE data no longer accessible outside of Google Sheets”. By this situation, #N/A had been returned when the value had been retrieved using a script.

Reducing Image Data Size using Google Apps Script

Gists

This is a sample script for reducing the image data size using Google Apps Script. You might have a situation where you might want to reduce the data size of image data using Google Apps Script. Here, using Google Apps Script, I would like to introduce a sample script for reducing the data size of the image data by reducing the image quality.

Limitations

In the current stage, by the specification of Google side, there are the following limitations.

Expanding Rows in Google Spreadsheet using Google Apps Script

Gists

This is a sample script for expanding the rows in Google Spreadsheet using Google Apps Script. The sample situation is as follows.

Sample situation

Input

Expanding Rows in Google Spreadsheet using Google Apps Script

Output

Expanding Rows in Google Spreadsheet using Google Apps Script

Sample script

function myFunction() {
  const expandedColumns = [2, 3, 4, 5]; // Please set the expanded columns you expect.
  const delimiter = "\n"; // Please set the delimiter.
  const srcSheetName = "Sheet1"; // Please set the source sheet name.
  const dstSheetName = "Sheet2"; // Please set the destination sheet name.

  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const [srcSheet, dstSheet] = [srcSheetName, dstSheetName].map((s) =>
    ss.getSheetByName(s)
  );
  const [head, ...values] = srcSheet.getDataRange().getValues();
  const res = [
    head,
    ...values.flatMap((r) => {
      const { v, max } = expandedColumns.reduce(
        (o, c, i) => {
          const s = r[c - 1].split(delimiter);
          o.v[c - 1] = s;
          const len = s.length;
          if (i == 0) {
            o.max = len;
          } else {
            o.max = o.max > len ? o.max : len;
          }
          return o;
        },
        { v: {}, max: 0 }
      );
      return [...Array(max)].map((_, j) =>
        r.map((c, k) => (!v[k] ? c : v[k][j] || null))
      );
    }),
  ];
  dstSheet.getRange(1, 1, res.length, res[0].length).setValues(res);
}
  • When this script is run, the above sample situation can be obtained.
  • For example, when you change const expandedColumns = [2, 3, 4, 5]; to const expandedColumns = [5];, only the column “E” is expanded.

Number of Requests for Sheets API using Google Apps Script

Gists

This is a report for checking the number of requests for Sheets API. I had contact about the quota for Sheets API. So, in order to explain this, I used the following simple sample scripts.

Sample 1

This sample puts a value of “sample” to a cell “A1” using the batchUpdate method. This request body includes one request. When this script is run, one API quota is used.

function sample1() {
  const spreadsheetId = "###";
  const sheetId = 0;

  const requests = [{
    "updateCells": {
      "rows": [
        {
          "values": [
            {
              "userEnteredValue": {
                "stringValue": "sample"
              }
            }
          ]
        }
      ],
      "range": {
        "sheetId",
        "startRowIndex": 0,
        "endRowIndex": 1,
        "startColumnIndex": 0,
        "endColumnIndex": 1
      },
      "fields": "userEnteredValue.stringValue"
    }
  }];
  Sheets.Spreadsheets.batchUpdate({ requests }, spreadsheetId);
}

Sample 2

This sample puts 1000 values like “sample#” to the cells of “B1:B1000” using the batchUpdate method. This request body includes 1000 requests. When this script is run, one API quota is used.

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.

Increasing Column Letter by One using Google Apps Script

Gists

This is a sample script for increasing the column letter by one using Google Apps Script.

Sample script

const increase = (ar) =>
  ar.map((e) => {
    const idx = [...e].reduce(
      (c, e, i, a) =>
        (c += (e.charCodeAt(0) - 64) * Math.pow(26, a.length - i - 1)),
      -1
    );

    // Ref: https://stackoverflow.com/a/53678158
    columnIndexToLetter = (n) =>
      (a = Math.floor(n / 26)) >= 0
        ? columnIndexToLetter(a - 1) + String.fromCharCode(65 + (n % 26))
        : "";

    return columnIndexToLetter(idx + 1);
  });

const samples = ["A", "Z", "AA", "AZ", "ZZ"];
const res = increase(samples);
console.log(res); // <--- [ 'B', 'AA', 'AB', 'BA', 'AAA' ]
  • When this script is used, the column letters of ["A", "Z", "AA", "AZ", "ZZ"] is increased by one. As the result, [ 'B', 'AA', 'AB', 'BA', 'AAA' ] is obtained.

Testing

GAS Library - DocNamedRangeApp

Overview

GAS Library - DocNamedRangeApp

This is a Google Apps Script library for managing the named range on Google Documents.

Description

Google Document can use the named range. When the named range is used, the users can directly access the contents using the named range. For example, the developer can guide the users to the specific content in Google Documents using the named range. But, unfortunately, in the current stage, it seems that the named range cannot be directly used by the UI on Google Documents. It is required to use Google Apps Script. But, I thought that when I saw the official document of the named range, it might be difficult to understand it. Ref So, I created this library. When this library is used, the named ranges on Google Documents can be managed.

Analyzing Responses from Grid Items of Google Form using Google Apps Script

Gists

This is a sample script for analyzing the responses from Grid Items of Google Form using Google Apps Script.

In this sample situation, all responses are retrieved from Grid Items of Google Form, and the average values of each row of each question are calculated. And, the result is put on the Spreadsheet.

Sample situation

Input: Sample Google Form

The sample Google Form is as follows.

Analyzing Responses from Grid Items of Google Form using Google Apps Script

Report: Handling 10,000,000 cells in Google Spreadsheet using Google Apps Script

Gists

Introduction

On March 14, 2022, it reported about “Google Sheets doubles cell limit”. Ref By this update, now, the users can use 10,000,000 cells in a Google Spreadsheet. This is great news for us. When I tried to handle 10,000,000 cells in a Google Spreadsheet using Google Apps Script, it was found that there were various important points. In this report, I would like to introduce the important points for handling 10,000,000 cells in Google Spreadsheet using Google Apps Script.