From 718645befc4de2ef4cb93923a51b40d9a529d87b Mon Sep 17 00:00:00 2001 From: AlidaOdendaal Date: Tue, 23 Nov 2021 22:58:58 +0200 Subject: [PATCH 1/2] spreadsheet: Reconfigure readSheet function Change-type: minor Signed-off-by: AlidaOdendaal --- lib/index.ts | 2 +- lib/spreadsheet.ts | 51 ++++++++++++++++++++++++++++++++++++---------- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/lib/index.ts b/lib/index.ts index 6230dfc..818e818 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -3,5 +3,5 @@ export { clearSheet, updateSheet, writeToSheet, - readGoogleSheetTab, + readSheet, } from './spreadsheet'; diff --git a/lib/spreadsheet.ts b/lib/spreadsheet.ts index 95ac3e2..169ae0e 100644 --- a/lib/spreadsheet.ts +++ b/lib/spreadsheet.ts @@ -84,15 +84,44 @@ export function writeToSheet( .setValues(writeArray); } -/** - * Generic function for reading content of tab in Google Sheet. - * @param tabName Name of tab in Google Sheet. - * @returns Raw sheet content as nested array. - */ -export function readGoogleSheetTab(tabName: string): any[][] { - const tab = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(tabName); - const rawData = tab - ?.getRange(1, 1, tab.getMaxRows(), tab.getMaxColumns()) - ?.getValues(); - return rawData || []; +export function readSheet( + sheetName: string, + { + range, + spreadSheet, + }: { + range?: string | [number, number, number, number]; + spreadSheet?: GoogleAppsScript.Spreadsheet.Spreadsheet; + } = {}, +): any[][] { + // If no spreadsheet provided, read the currently active spreadsheet: + if (!spreadSheet) { + spreadSheet = SpreadsheetApp.getActiveSpreadsheet(); + } + const spreadSheetName = spreadSheet.getName(); + const sheet = spreadSheet.getSheetByName(sheetName); + + // Check for existence of sheet in spreadsheet: + if (!sheet) { + throw new Error( + `Sheet "${sheetName}" does not exist in spreadsheet "${spreadSheetName}"!`, + ); + } + + // If no range provided, default to the full data-containing range in the sheet: + if (!range) { + range = [1, 1, sheet.getLastRow(), sheet.getLastColumn()]; + } + + // Read data from sheet: + let rangeData: any[][]; + if (typeof range === 'string') { + rangeData = sheet.getRange(range).getValues(); + } else { + rangeData = sheet.getRange(...range).getValues(); + } + console.log( + `Successfully read contents of sheet "${sheetName}" in spreadsheet "${spreadSheetName}".`, + ); + return rangeData; } From 3c135475419adf2de1d14c1cddda4a01f483af00 Mon Sep 17 00:00:00 2001 From: AlidaOdendaal Date: Wed, 1 Dec 2021 23:18:10 +0200 Subject: [PATCH 2/2] Allow tuples of different lengths in getRange, apply minor renaming Change-type: patch Signed-off-by: AlidaOdendaal --- lib/spreadsheet.ts | 50 +++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/lib/spreadsheet.ts b/lib/spreadsheet.ts index 169ae0e..f714ca4 100644 --- a/lib/spreadsheet.ts +++ b/lib/spreadsheet.ts @@ -1,3 +1,8 @@ +type RangeTuple = + | [number, number] + | [number, number, number] + | [number, number, number, number]; + export function clearSheet( sheet: GoogleAppsScript.Spreadsheet.Sheet, startingRow: number = 1, @@ -88,40 +93,43 @@ export function readSheet( sheetName: string, { range, - spreadSheet, + ss, }: { - range?: string | [number, number, number, number]; - spreadSheet?: GoogleAppsScript.Spreadsheet.Spreadsheet; + range?: string | RangeTuple; + ss?: GoogleAppsScript.Spreadsheet.Spreadsheet; } = {}, -): any[][] { +) { // If no spreadsheet provided, read the currently active spreadsheet: - if (!spreadSheet) { - spreadSheet = SpreadsheetApp.getActiveSpreadsheet(); + if (!ss) { + ss = SpreadsheetApp.getActiveSpreadsheet(); } - const spreadSheetName = spreadSheet.getName(); - const sheet = spreadSheet.getSheetByName(sheetName); + const ssName = ss.getName(); + const sheet = ss.getSheetByName(sheetName); // Check for existence of sheet in spreadsheet: if (!sheet) { throw new Error( - `Sheet "${sheetName}" does not exist in spreadsheet "${spreadSheetName}"!`, + `Sheet "${sheetName}" does not exist in spreadsheet "${ssName}"!`, ); } - - // If no range provided, default to the full data-containing range in the sheet: + // Read data from sheet (if no range provided, default to the full data-containing range in the sheet): + let rangeValues: any[][] = [] if (!range) { - range = [1, 1, sheet.getLastRow(), sheet.getLastColumn()]; - } - - // Read data from sheet: - let rangeData: any[][]; - if (typeof range === 'string') { - rangeData = sheet.getRange(range).getValues(); + rangeValues = sheet.getDataRange().getValues(); } else { - rangeData = sheet.getRange(...range).getValues(); + // The extended if-else blocks below constitute a workaround for this issue: https://github.com/microsoft/TypeScript/issues/14107 + if (typeof range === 'string') { + rangeValues = sheet.getRange(range).getValues(); + } else if (range.length === 2) { + rangeValues = sheet.getRange(...range).getValues(); + } else if (range.length === 3) { + rangeValues = sheet.getRange(...range).getValues(); + } else if (range.length === 4) { + rangeValues = sheet.getRange(...range).getValues(); + } } console.log( - `Successfully read contents of sheet "${sheetName}" in spreadsheet "${spreadSheetName}".`, + `Successfully read contents of sheet "${sheetName}" in spreadsheet "${ssName}".`, ); - return rangeData; + return rangeValues; }