Skip to content

Instantly share code, notes, and snippets.

View ttsukagoshi's full-sized avatar

TSUKAGOSHI Taro ttsukagoshi

View GitHub Profile
@ttsukagoshi
ttsukagoshi / moveAllFiles.gs
Created December 30, 2019 14:45
Google App Script (Javascript) to move all files in a particular Google Drive folder to another folder
/**
* Function to move all files in a particular Google Drive folder to another folder
* @param {string} moveFromFolderId - the original Google Drive folder ID from which you want to move the files
* @param {string} moveToFolderId - the Google Drive folder ID to which you want to move the files
*/
function moveAllFiles(moveFromFolderId, moveToFolderId) {
var moveFrom = DriveApp.getFolderById(moveFromFolderId);
var moveTo = DriveApp.getFolderById(moveToFolderId);
var files = moveFrom.getFiles();
@ttsukagoshi
ttsukagoshi / dynamic_choices_for_Google_Form.gs
Last active January 9, 2020 03:08
GoogleフォームにバインドされたGoogle App Script (GAS)。ラジオボタン型の設問に対する選択肢を、一人が選択してフォーム送信したときに、以降の回答者からはその選択肢が見えないようにする。Google Form-bound app script for making a form with dynamic choices, i.e., when one respondent of the form selects a choice in a radio button-type question, that choice is deleted for later respondents.
//元の選択肢。
var originalChoice = [
'Choice1',
'Choice2',
'etc.'
];
// 設定
var numQuestion = 2; // 動的に変化させる質問の番号。配列なので0からカウント。0, 1, 2, ...
var textQuestion = 'Title of Item (form question)'; // 動的に変化させる質問のテキスト
@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);
@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 / 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 / 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 / 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 / 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 / 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 / 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) {