Gists
This is a sample script for splitting the binary data with search data using Google Apps Script.
Sample script
/**
* Split byteArray by a search data.
* @param {Array} baseData Input byteArray of base data.
* @param {Array} searchData Input byteArray of search data using split.
* @return {Array} An array including byteArray.
*/
function splitByteArrayBySearchData_(baseData, searchData) {
if (!Array.isArray(baseData) || !Array.isArray(searchData)) {
throw new Error("Please give byte array.");
}
const search = searchData.join("");
const bLen = searchData.length;
const res = [];
let idx = 0;
do {
idx = baseData.findIndex(
(_, i, a) => [...Array(bLen)].map((_, j) => a[j + i]).join("") == search
);
if (idx != -1) {
res.push(baseData.splice(0, idx));
baseData.splice(0, bLen);
} else {
res.push(baseData.splice(0));
}
} while (idx != -1);
return res;
}
// Please run this function.
function main() {
const sampleString = "abc123def123ghi123jkl";
const splitValue = "123";
const res1 = splitByteArrayBySearchData(
...[sampleString, splitValue].map((e) => Utilities.newBlob(e).getBytes())
);
const res2 = res1.map((e) => Utilities.newBlob(e).getDataAsString());
console.log(res1); // [[97,98,99],[100,101,102],[103,104,105],[106,107,108]]
console.log(res2); // ["abc","def","ghi","jkl"]
}
-
When main() is run, the sample input values of "abc123def123ghi123jkl" is split by "123". And, [[97,98,99],[100,101,102],[103,104,105],[106,107,108]] is obtained. In this case, when [[97,98,99],[100,101,102],[103,104,105],[106,107,108]] is converted to the string for confirming the result value, it becomes ["abc","def","ghi","jkl"].
Gists
This is a sample script for exporting Google Docs files (Spreadsheets, Documents, and so on) in PDF format with batch requests using Google Apps Script.
I have published a report “Efficient File Management using Batch Requests with Google Apps Script”. In this report, I mentioned how to use the batch requests using Google Apps Script.
In this post, I would like to introduce the method for retrieving binary data using this method.
BatchRequest was updated to v1.2.1.
-
v1.2.1 (March 8, 2023)
- An option of
exportDataAsBlob was added to the request object to the method of EDo(). Ref When this option is used, the response values from the batch requests are returned as Blob. By this, for example, when you export Google Spreadsheet as PDF data using the batch requests, the PDF data can be retrieved as Blob.
Sample script using exportDataAsBlob
In this sample, the Spreadsheet and Document files are exported as PDF format using the batch requests. The exported PDF data is created as a PDF file to the root folder. When I answered this thread on Stackoverflow, when this option is added to this library, I thought that it might be useful for users.
Gists
This is a sample script for achieving the resumable download of a file from Google Drive using Dive API with Python.
There might be a case in that you want to achieve the resumable download of a file from Google Drive using Dive API with Python. For example, when a large file is downloaded, the downloading might be stopped in the middle of downloading. At that time, you might want to resume the download. In this post, I would like to introduce the sample script of python.
Gists
This is a sample script for retrieving the total file sizes in the specific folder of Google Drive using Google Apps Script.
There is a case where you want to retrieve the total file sizes in the specific folder of Google Drive using Google Apps Script. In this post, I would like to introduce a sample script for achieving this.
Sample script
Before you use this script, please enable Drive API at Advanced Google services. And please install FilesApp of a Google Apps Script library.
Gists

This is a sample script for retrieving the start and end row numbers of the same values in a column on Google Spreadsheet using Google Apps Script.
There is a case in that I want to retrieve the rows of the same values in a column on Google Spreadsheet using Google Apps Script. In this post, I would like to introduce a simple sample script for achieving this.
Gists
When Google APIs are used with googleapis for Python, the client is obtained as follows.
creds = service_account.Credentials.from_service_account_file(service_account_credential_file, scopes=scopes)
service = build("drive", "v3", credentials=creds)
In this case, when the script is run, the access token is retrieved every time. But, the expiration time of the retrieved access token is 1 hour. Here, there might be the case that you want to use the access token until the expiration time. It is considered that effectively using the access token will lead to SDGs. In this post, I would like to introduce a sample script for using the access token until the expiration time.
Gists
Today, I discussed with Riël Notermans an issue with the HTML form with the input tab of type="file" with google.script.run. Through this discussion, the reason for this issue could be found. When you use the input tab of type="file" in the HTML form, and you want to send the file content with google.script.run, I thought that this post might be useful for other users. So, I posted it here.
Gists
This is a sample script for retrieving the access token from the service account using oauth2client and google-auth with Python.
Sample script 1
Use oauth2client.
from oauth2client.service_account import ServiceAccountCredentials
SERVICE_ACCOUNT_FILE = "credentials.json"
SCOPES = ["https://www.googleapis.com/auth/drive"]
creds = ServiceAccountCredentials.from_json_keyfile_name(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
res = creds.get_access_token()
access_token = res.access_token
print(access_token)
Sample script 2
Use google-auth. In the current stage, this method might be general.
from google.oauth2 import service_account
import google.auth.transport.requests
SERVICE_ACCOUNT_FILE = "credentials.json"
SCOPES = ["https://www.googleapis.com/auth/drive"]
creds = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
request = google.auth.transport.requests.Request()
creds.refresh(request)
access_token = creds.token
print(access_token)
References
Gists
This sample script decrypts the salted base64 data of finance.yahoo.com using Google Apps Script.
Recently, it seems that the specification of the key for decrypting the data has been changed on the server side, again. In this update, I looked for the logic for retrieving the key value. But, I cannot still find it. So, in this post, I would like to use a workaround discussed in this thread. In this thread, the valid keys are listed in a text file. Using this, I updated the script as follows.