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

The Thinker

Workaround: Smart Chips with Google Apps Script

Gists

Description

Now, Google Docs and Google Sheets can insert smart chips. Smart chips are very useful for easily inserting information like users, maps, files, and so on. However, unfortunately, at the current stage, smart chips cannot be directly managed using Google Apps Script. Specifically, the information within smart chips cannot be directly retrieved by Google Apps Script. Although I believe this will be resolved in a future update, there might be cases where you want to retrieve information from smart chips using Google Apps Script. This report introduces a workaround for achieving this.

Creating Tree Structure from Headings in Google Documents using Google Apps Script

Gists

Description

This is a Google Apps Script for creating the tree structure from headings in Google Documents.

Usage

In order to test this script, please do the following steps.

1. Sample Document

Create a sample Document as follows.

2. Sample script

Open the script editor and copy and paste the following script.

In this script, Document service (DocumentApp) is used.

function sample1() {
  const headTypes = [
    "TITLE",
    "HEADING1",
    "HEADING2",
    "HEADING3",
    "HEADING4",
    "HEADING5",
    "HEADING6",
  ];
  const body = DocumentApp.getActiveDocument().getBody();
  const res = body
    .getParagraphs()
    .reduce((ar, p) => {
      const idx = headTypes.indexOf(p.getHeading().toString());
      if (idx > -1) {
        ar.push(Array(idx).fill(" ").join("") + p.getText());
      }
      return ar;
    }, [])
    .join("\n");
  console.log(res);
}

As another approach, the following script uses Docs API. Please enable Docs API at Advanced Google services. Ref

Technique for Changing Glyph Colors of Bullets in Google Documents Using Google Apps Script

Gists

Description

This report introduces a sample script for changing the glyph colors of bullets in lists on Google Documents using Google Apps Script. Currently, there are no built-in methods for achieving this using either the Document service (DocumentApp) or the Docs API. However, this can be accomplished through a specific process. This report will introduce that process using Google Apps Script.

Principle

When the foreground color of the text within a list is changed using the Google Document service (DocumentApp), the bullet glyphs are not affected. However, when the foreground color of all text within a list is changed using the Google Docs API, the bullet glyphs are changed. This script leverages this behavior. The detailed process is as follows:

Trend of google-apps-script Tag on Stackoverflow 2025

Gists

Published: March 9, 2025

Kanshi Tanaike

Introduction

On Stack Overflow, numerous users post questions and answers daily across various tags. These discussions, spanning a wide range of topics, provide valuable information and are highly beneficial. One such tag is “google-apps-script,” where I occasionally participate in discussions. Observing these threads over time, it’s evident that they evolve alongside updates to Google Apps Script, the technology at the core of the tag. This report aims to analyze this evolution as a trend within the “google-apps-script” tag. This trend encompasses changes in the number of questions, questioners, answerers, and associated tags. The evolution of the “google-apps-script” tag is closely linked to the development of Google Apps Script and its diverse applications.

Statistics of my Activities from 2024-01-01 - 2024-12-31 on Stackoverflow

URL: https://stackoverflow.com/users/7108653/tanaike?tab=answers&sort=newest

This is the statistics of my activities from 2024-01-01 - 2024-12-31 on Stackoverflow.

  • Answers to stackoverflow

    • I answered 380 answers.
    • On March 02, 2025, the total view count is 182,695.
  • Answers to ja.stackoverflow

    • I answered 2 answers.
    • On March 02, 2025, the total view count is 3,409.

Total view counts: 186,104

Workaround: Export web-published Google Docs as PDFs using Google Apps Script

Gists

Abstract

This report outlines a Google Apps Script solution for directly exporting web-published Google Docs to PDF. By circumventing limitations in published URLs, the script enables convenient PDF generation without manual intervention.

Introduction

Google Sheets and Google Docs offer the convenient feature of web publishing, providing readily accessible URLs for sharing. Ref

  • Google Sheets: https://docs.google.com/spreadsheets/d/e/2PACX-###/pubhtml
  • Google Docs: https://docs.google.com/document/d/e/2PACX-###/pub

These URLs utilize a unique ID (###) for each document. While Google Sheets supports direct PDF export by modifying the URL to https://docs.google.com/spreadsheets/d/e/2PACX-###/pub?output=pdf, a similar direct method for Google Docs is currently unavailable.

Streamlining Gmail Processing Including Attachment Files Using Gemini with Google Apps Script

Gists

Abstract

A new library, MimeTypeApp, simplifies using Gmail messages and attachments with the Gemini API for tasks like text analysis. It converts unsupported formats for seamless integration with Google Apps Script and Gemini.

Introduction

Recently, I published MimeTypeApp, a Google Apps Script library that simplifies parsing Gmail messages, including attachments, for use with the Gemini API. Ref This library addresses a key challenge: Gmail attachments come in various MIME types, while the Gemini API currently only accepts a limited set for processing. MimeTypeApp bridges this gap by providing functions to convert unsupported MIME types to formats compatible with Gemini. With MimeTypeApp, you can streamline your workflows that involve parsing Gmail messages and their attachments for tasks like text extraction, summarization, or sentiment analysis using the Gemini API. This report introduces a sample script that demonstrates how to leverage MimeTypeApp to achieve this functionality. By leveraging Google Apps Script’s integration capabilities, MimeTypeApp allows you to create powerful applications that seamlessly connect Gmail, Spreadsheets (for storing results or extracted data), and the Gemini API.

MimeTypeApp: Flexible MimeType Converter with Google Apps Script

Gists

Abstract

This is a Google Apps Script library for converting files from various MIME types to a specified target MIME type. The library accepts both file IDs and blobs as input values.

Introduction

Recently, I encountered a scenario where I needed to convert files of various MIME types to a specific target MIME type. While converting files with known source MIME types is relatively straightforward, the process becomes more complex when the source MIME type is unknown. To simplify this task, I developed a Google Apps Script solution that can effectively convert files of diverse MIME types to a desired target MIME type. The script can accept both file IDs and blobs as input values.

Updated: GAS Library - GeminiWithFiles

GeminiWithFiles was updated to v2.0.3

  • v2.0.3 (November 19, 2024)

    1. I modified the specification of setFileIdsOrUrlsWithResumableUpload. From v2.0.3, when you use this method, please include propertiesService: PropertiesService.getScriptProperties() into the initial object as follows. Because, when PropertiesService.getScriptProperties() is used in the library, the values are put into the library. When I created Ref and Ref, I supposed that the script is used by copying and pasting instead of the library. So, I included PropertiesService.getScriptProperties() in the script. But I noticed that when this is used with GeminiWithFiles, each user is required to use PropertiesService.getScriptProperties(). So, I modified this.
    2. As an additional option, when you want to upload the data with the resumable upload as a new upload, please set resumableUploadAsNewUpload: true. Ref By this, the property is cleared and the upload is run.

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