Removing Vertical Borders of Table in Google Document using Google Apps Script

Gists

Removing Vertical Borders of Table in Google Document using Google Apps Script

These are sample scripts for removing the vertical borders of a table in Google Document using Google Apps Script.

Unfortunately, in the current stage, only the vertical borders cannot be removed from the table in Google Document using the Google Document service (DocumentApp). I believe that this might be resolved in the future update. But, in the current stage, when Google Docs API is used, this can be achieved. So, Google Docs API can be used as the current workaround. But, I thought that the combination of Google Document service (DocumentApp) and Google Docs API might be a bit complicated. So, I would like to introduce 3 sample scripts.

Retrying UrlFetchApp by an Error using Google Apps Script (RetryFetch)

Gists

This is a sample script for retrying UrlFetchApp of Google Apps Script when an error occurs.

When the HTTP request is run using UrlFetchApp, there is a case that an error occurs in various situations. And, there is a case that when the request is run again, no error occurs. This sample script can automatically retry the requests using Google Apps Script.

Sample script

This is Class RetryFetch.

/**
 * UrlFetchApp is run by retrying when an error occurs.
 */
class RetryFetch {
  /**
   * @param {string} url URL
   * @param {object} params Object
   * @param {number} numberOfRetr Number of retry when an error occurs with the HTTP request.
   * @param {number} waitTime Wait time between the HTTP request.
   * @return {UrlFetchApp.HTTPResponse}
   */
  constructor(url, params = {}, numberOfRetry = 2, waitTime = 3) {
    this.url = url;
    if (!params.muteHttpExceptions) {
      params.muteHttpExceptions = true;
    }
    this.params = params;
    this.numberOfRetry = numberOfRetry;
    this.waitTime = waitTime;
    this.his = [];
  }

  fetch() {
    const res = UrlFetchApp.fetch(this.url, this.params);
    const statusCode = res.getResponseCode();
    this.his.push({ date: new Date(), params: this.params, statusCode });
    if (statusCode != 200 && this.numberOfRetry > 0) {
      console.log(`Status code: ${statusCode}, Retry: ${this.numberOfRetry}`);
      const idx = this.his.length - 1;
      this.his[idx].responseHeader = res.getAllHeaders();
      this.his[idx].error = res.getContentText();
      this.numberOfRetry--;
      Utilities.sleep(this.waitTime * 1000);
      this.fetch();
    } else if (this.numberOfRetry == 0) {
      return null;
    }
    return res;
  }

  /**
   * Return history of fetch requesting in this Class.
   * @return {array} History.
   */
  get history() {
    return this.his;
  }
}

This is a sample script for using Class RetryFetch.

Requesting with Keeping Cookies using Google Apps Script (SessionFetch)

Gists

This is a sample script for requesting with keeping cookies using Google Apps Script.

This might be similar to requests.Session" of Python. Ref

Sample script

This is Class SessionFetch.

/**
 * UrlFetchApp is run by keeping Cookie.
 */
class SessionFetch {
  constructor() {
    this.cookie = "";
    this.his = [];
  }

  /**
   * Request URL.
   * @param {string} url URL
   * @param {object} params Object
   * @return {UrlFetchApp.HTTPResponse}
   */
  fetch(url, params = {}) {
    if (this.cookie != "") {
      if (!params.headers) {
        params.headers = { Cookie: this.cookie };
      } else if (!params.headers["Cookie"]) {
        params.headers["Cookie"] = this.cookie;
      }
    }
    this.his.push({ date: new Date(), url, params });
    const res = UrlFetchApp.fetch(url, params);
    this.cookie = res.getAllHeaders()["Set-Cookie"] || "";
    return res;
  }

  /**
   * Return history of fetch requesting in this Class.
   * @return {array} History.
   */
  get history() {
    return this.his;
  }
}

This is a sample script for using Class SessionFetch.

Retrieving Files of 'Shared with Me' in Specific Folder using Google Apps Script

Gists

This is a sample script for retrieving the files of ‘Shared with Me’ in the specific folder using Google Apps Script.

In the current stage, when you transfer the ownership of your file on your Google Drive to another user and/or copy the file “Shared with me” to the specific folder on your Google Drive, the file becomes the shortcut file. Under this situation, when you want to retrieve the files of “Shared with me” in the specific folder, unfortunately, the following search query cannot be used.

Full-text search of Google Apps Script Projects using Google Apps Script

Gists

Full-text search of Google Apps Script Projects using Google Apps Script

These are sample scripts for achieving the full-text search of Google Apps Script projects using Google Apps Script. I have the case that I want to search a value from Google Apps Script projects using a Google Apps Script. In this post, I would like to introduce the sample scripts for achieving the full-text search of Google Apps Script projects.

Benchmark: Process Costs for Checking Value in Array using Google Apps Script

Gists

Kanshi Tanaike

Introduction

There is a maximum executing time for Google Apps Script (GAS). That is 6 minutes. And, in the case of the custom function and the simple trigger, it is 30 seconds. Ref So users always have to pay attention to reducing the process cost of the scripts. Especially, it is very important to know the process costs for the array processing, because array processing is often used for spreadsheets and Google APIs. I have already reported about the array processing at “Benchmark: Loop for Array Processing using Google Apps Script with V8” and “Search for Array Processing using Google Apps Script”. In this report, the process cost checking a value in a one-dimensional array using Google Apps Script has been investigated.

Converting Large images to Google Document by OCR using Google Apps Script

Gists

Converting Large images to Google Document by OCR using Google Apps Script

This is a sample script for converting the large images to Google Document by OCR using Google Apps Script.

When the image size, the image file size, the resolution of the image, and so on are large, an error like Request Too Large occurs. In this sample script, such the image can be converted to Google Document by reducing them.

Detecting Cells with Quote Prefix in Google Spreadsheet using Google Apps Script

Gists

Detecting Cells with Quote Prefix in Google Spreadsheet using Google Apps Script

This is a sample script for detecting cells with the quote prefix in Google Spreadsheet using Google Apps Script.

For example, when a value is put to a cell by adding a single quote ' as the top character, the cell value is used as the string value. This is the current specification. Under this condition, when the cells with the value of the quote prefix are tried to be detected, unfortunately, in the current stage, it seems that there is no method for directly achieving this in the methods of Spreadsheet service (SpreadsheetApp).

Report: Rule of Item IDs for Questions of Google Forms

Gists

This is a report related to the rule of item IDs for questions of Google Forms.

When the questions are created using the method of batchUpdate with Google Forms API, the created questions have the item IDs when the item IDs are not given in the request body. ( https://developers.google.com/forms/api/reference/rest/v1/forms#item ) For example, when you want to create a question and update the created question in one API call, it is required to include the custom item ID in the request body. But, it seems that in the current stage, the information of the item ID has never been published. So, in this report, I would like to consider the rule of item IDs of questions for Google Forms API.