Modify Shading Color of Paragraph on Google Document using Google Apps Script

Gists

This is a script for modifying the shading color of paragraph on Google Document using Google Apps Script.

Recently, by releasing Google Docs API, the shading color got to be able to be modified using the script. Here, I would like to introduce a sample script for modifying the shading color of the paragraph on Google Document. At the current Document Service, the shading color cannot be modified yet. I think that this will be achieved in the future update. At that time, I think that a new property might be added to Enum Attribute.

Adding Title of vAxis to Embedded Chart on Spreadsheet using Google Apps Script

Gists

When a chart is created by using EmbeddedChartBuilder of Spreadsheet service, the title of vAxis which is put by setOption("vAxis", {title: "y axis"}) doesn’t work. It is considered that this is a bug. Because I have confirmed that this had worked fine. Ref But this specification had been changed. So I would like to introduce the method for adding the title of vAxis when a chart is created using Google Apps Script.

tarUnarchiver for Google Apps Script

Overview

This is a script for extracting files from a tar file using Google Apps Script. This script was created by native Google Apps Script.

The following 3 situations gave me the motivarion for creating this script.

  1. Although I had been looking for the script for extracting files from a tar file from before, I have still not been able to find it.
  2. Unfortunately, there are no methods for extracting the files from the tar file in Google Apps Script. But fortunately, from wiki of tar, I could retrieve the structure information of the tar data. And I can also study by creating this script.
  3. I found this thread at Stackoverflow. By this, I could understand that other users also want the script for extracting files from the tar file.

So I created this.

Sample Script for Executing with Synchronous Process using Node.js

Gists

This is a sample script for executing with the synchronous process using Node.js.

Sample script

function work(e) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            console.log(e);
            resolve("ok" + e);
        }, 1000);
    });
}

async function main() {
    var ar = [1, 2, 3, 4, 5];
    for (var i = 0; i < ar.length; i++) {
        console.log('start' + ar[i]);
        await work(ar[i]).then((res) => console.log(res));
        console.log('end' + ar[i]);
    }
}

main(); // Run main().

Result

start1
1
ok1
end1
start2
2
ok2
end2
start3
3
ok3
end3
start4
4
ok4
end4
start5
5
ok5
end5

GAS Library - ProcessApp

Overview

This is a library for retrieving the process and information of Google Apps Script.

Methods

  1. getExecutionTimeOfTrigger() : This method retrieves the total execution time of all functions executed by the time-driven trigger at owner’s account. For example, you can know the total execution time of all functions executed by the time-driven trigger in 24 h.
  2. getDevUrl() : This method retrieves the endpoint of developer mode for Web Apps like https://script.google.com/macros/s/#####/dev.
  3. getRunningFunctions() : This method retrieves the functions which are running now.

You can check this at https://github.com/tanaikech/ProcessApp.

Creating One-time Writing Cells using Google Apps Script

Gists

This sample script is for creating one-time writing cells using Google Apps Script. At first, it supposes the following situation.

  1. A Spreadsheet is shared with users. The owner of Spreadsheet is you.
  2. After users put a value to a cell, you don’t want to make users edit the cell again.
    • Namely, you want to protect the cell.

This sample script achieves above situation.

Preparation

Before you use this script, please do the following flow.

Update: Taking advantage of Web Apps with Google Apps Script

“Taking advantage of Web Apps with Google Apps Script” was updated. A section of “How to use dev mode from outside” was added.

  • When you deploy Web Apps, you can see the link labeled latest code. The link is like https://script.google.com/macros/s/###/dev. When you access to the link of latest code using your browser under you login to Google, you can access to Web Apps with the dev mode. But if you want to access to Web Apps with the dev mode from outside, there are no documents for the method. Here, it introduces such method.

You can check this at https://github.com/tanaikech/taking-advantage-of-Web-Apps-with-google-apps-script.

Opening Dialog Box during Calculation and Retrieving Calculated Result using Google Apps Script

Gists

  1. When it starts a calculation, open a dialog box.
  2. When the calculation is finished, close the dialog and retrieve the calculated result.

This is a sample script for achieving above flow. This sample script supposes to use the container-bound script of Spreadsheet. When you use this, please run the function of run().

Sample script:

function doSomething(e) {

  // Scripts for calculating.

  Utilities.sleep(3000); // This is a sample wait time.
  var data = "data";
  main({result: data});
}

function openDialogue() {
  var html = "<script>google.script.run.withSuccessHandler(function() {google.script.host.close()}).doSomething();</script>";
  var h = HtmlService.createHtmlOutput(html);
  SpreadsheetApp.getUi().showModalDialog(h, "Sample");
}

function main(e) {
  if ("result" in e) return e.result;
  openDialogue(e);
}

// Please run this function.
function run() {
  var res = main({});
    Logger.log(res);
}

Note:

  • This can be also used for the sidebar and other Google Docs.

Closing Existing Sidebar using Google Apps Script

Gists

This is a sample script for closing the existing sidebar using Google Apps Script. When the sidebar is opened, in order to close the sidebar, the sidebar can be closed by running google.script.host.close() with the script of sidebar. This is the general case.

If you want to close the opened sidebar, such functions are not prepared. So I thought this workaround. The flow of this workaround is as follows.