Skip to content

Instantly share code, notes, and snippets.

View ttsukagoshi's full-sized avatar

TSUKAGOSHI Taro ttsukagoshi

View GitHub Profile
@ttsukagoshi
ttsukagoshi / choice-editor-for-google-form.js
Created November 18, 2020 03:46
A Google Apps Script sample for a simple solution to manage the choices in list or multiple-choice items of a specific Google Form using Google Sheets.
const CONFIG = {
'sheetName': 'Choices', // Name of sheet
'cellFormUrl': { 'rowNum': 4, 'colNum': 2 }, // Row and column numbers of the cell that designates the URL of Google Form.
'rangeOffsetChoices': { 'rowNum': 6, 'colNum': 3 }, // Left- and upper-most cell of the range in which choices are listed.
'timestampItemTitle': '最終更新日時' // Title of the item in the target Google Form that denotes the timestamp of which the form was last updated.
};
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('Choice Editor')
@ttsukagoshi
ttsukagoshi / localized-message-framework-for-gas.md
Last active November 17, 2020 15:32
Framework to include localized messages into Google Apps Scripts

How to Use

  1. Prepare a copy of the code in the localized-message.js, setting MESSAGES to suit your needs.
  2. To call a message,
// Get user locale
var locale = Session.getActiveUserLocale();

or alternatively, if you are creating a script bound to a Google Docs file (e.g., a Google Sheets file),

var locale = SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetLocale();
@ttsukagoshi
ttsukagoshi / cellPixSizes.js
Created October 15, 2020 03:12
Gets the cells' height and width in pixels for the selected range in Google Spreadsheet in form of a 2-d JavaScript array; the array values are ordered in the same way as executing Range.getValues()
/**
* Gets the cells' height and width in pixels for the selected range in Google Spreadsheet in form of a 2-d JavaScript array;
* the array values are ordered in the same way as executing Range.getValues()
* @param {Object} activeSheet The active Sheet class object in Google Spreadsheet, e.g., SpreadsheetApp.getActiveSheet()
* @param {Object} activeRange The selected Range class object in Google Spreadsheet, e.g., SpreadsheetApp.getActiveRange()
*/
function cellPixSizes(activeSheet, activeRange) {
var rangeStartCell = { 'row': activeRange.getRow(), 'column': activeRange.getColumn() };
var cellValues = activeRange.getValues();
var cellPixSizes = cellValues.map(function(row, rowIndex) {
@ttsukagoshi
ttsukagoshi / timestampOnEdit.js
Last active September 24, 2020 02:08
Automatically enter the timestamp for the latest update on the current Google spreadsheet.
const RECORDING_SHEET_NAME = 'SHEET_NAME'; // Sheet name to record the timestamp.
const RECORDING_CELL = {'row': 2, 'column': 2}; // Row and column number of the cell to record the timestamp.
/**
* Automatic timestamp for the latest update on this spreadsheet.
*/
function onEdit() {
var now = new Date();
var timestamp = now.toISOString();
SpreadsheetApp.getActiveSpreadsheet()
@ttsukagoshi
ttsukagoshi / deleteSheets.js
Last active November 17, 2020 15:34
Delete all sheets in this Google spreadsheet except for the designated sheet IDs.
/**
* Delete all sheets in this spreadsheet except for the designated sheet IDs
* @param {Array} exceptionSheetIds [Optional] Array of sheet IDs to not delete
*/
function deleteSheets(exceptionSheetIds) {
exceptionSheetIds = exceptionSheetIds || [];
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheets = ss.getSheets();
for (var i = 0; i < sheets.length; i++) {
var sheet = sheets[i];
@ttsukagoshi
ttsukagoshi / checkICalId.gs
Created January 18, 2020 17:26
Google App Script to check the iCalID of a specific Google Calendar event.
/**
* Google App Script to check the iCalID of a specific Google Calendar event.
* You need to have access to the Google Calendar, as designated by the Google Calendar ID
*/
function checkICalId() {
var calendarId = '[email protected]';
var startTime = new Date('2020-01-18T17:15+0900');
var endTime = new Date('2020-01-18T17:15+0900');
var events = CalendarApp.getCalendarById(calendarId).getEvents(startTime, endTime);
for (var i = 0; i < events.length; i++) {
@ttsukagoshi
ttsukagoshi / scriptPropertiesExport_Import.gs
Created January 18, 2020 16:29
Ever wanted to export & import script properties in Google App Scripts? Here's a simple copy & paste method.
/**
* Script for the original Google App Script from which you want to export the script properties.
* Shows in the log the script properties in JSON-encoded object; use with scriptPropertiesImport() described below
*/
function scriptPropertiesExport() {
var properties = JSON.stringify(PropertiesService.getScriptProperties().getProperties());
Logger.log(properties);
}
@ttsukagoshi
ttsukagoshi / getMax.gs
Last active January 17, 2020 04:22
Returns the maximum value in a designated column of a Google Spreadsheet
/**
* Returns the maximum value in a designated column
* @param {sheet} sheet Target sheet
* @param {number} numCol Column number of target column
* @param {number} initialValue Initial value
* @return {number} max The largest number in target column
*/
function getMax(sheet, numCol, initialValue) {
initialValue = initialValue || 0;
var data = sheet.getRange(2, numCol, sheet.getLastRow()-1).getValues(); // First row of sheet used as header row
@ttsukagoshi
ttsukagoshi / createSheets_.js
Last active June 1, 2020 02:39
Create Google Spreadsheet sheet(s) from set(s) of header and value
/**
* Create Google Spreadsheet sheet(s) from set(s) of header and value
*
* @param {Object} spreadsheet - Spreadsheet object to create sheet on
* @param {Array} dataSet - Array of formatted object containing data object(s) which should be in form of
* [
* {'sheetName':{string}'sheetName0', 'sheetData':{Array}dataObject0},
* {'sheetName':{string}'sheetName1', 'sheetData':{Array}dataObject1},...
* ]
* @param {string} prefix - Optional. Prefix to be added to title of each sheet.
@ttsukagoshi
ttsukagoshi / createSpreadsheet_.js
Last active June 1, 2020 02:45
Google App Script function to create a Google Spreadsheet in a particular Google Drive folder
/**
* Function to create a Google Spreadsheet in a particular Google Drive folder
* @param {Object} targetFolder - Google Drive folder object in which you want to place the spreadsheet
* @param {string} ssName - name of spreadsheet
* @return {string} ssId - spreadsheet ID of created spreadsheet
*/
function createSpreadsheet_(targetFolder, ssName) {
var ssId = SpreadsheetApp.create(ssName).getId();
var temp = DriveApp.getFileById(ssId);
targetFolder.addFile(temp);