Skip to content

Instantly share code, notes, and snippets.

@syakesaba
Created May 12, 2019 16:59
Show Gist options
  • Save syakesaba/4e2b71105335a152cd70d3322778e69e to your computer and use it in GitHub Desktop.
Save syakesaba/4e2b71105335a152cd70d3322778e69e to your computer and use it in GitHub Desktop.
GASでSpreadsheetを使う際にキャッシュアクセスをするスクリプト
//"data" has to be the name of a datasheet.
DEFAULT_DATA_SHEET_NAME = "data";
//Cached data sheet holder (save API access)
D = function() {
this.dataSheetName = DEFAULT_DATA_SHEET_NAME;
this.dataSheet = undefined;
this.data = undefined;
this.dataShape = undefined;
};
//Call this and save time.
function getD(force_reset) {
if (getD.d == undefined || force_reset)
getD.d = new D();
return getD.d;
}
//Sheet object
D.prototype.getDataSheet = function() {
if (this.dataSheet == undefined)
this.dataSheet = SpreadsheetApp.getActive().getSheetByName(this.dataSheetName);
return this.dataSheet;
}
//2D-Array
D.prototype.getData = function() {
if (this.data == undefined) {
var ds = this.getDataSheet();
this.data = ds.getDataRange().getValues();
}
return this.data;
}
//1D-Array
D.prototype.getDataShape = function() {
if (this.dataShape == undefined) {
var df = this.getData();
var row = df.length;
var col = df[df.length-1].length;
this.dataShape = [row, col];
}
return this.dataShape;
}
//UnitTest D
D.prototype.ut = function() {
Logger.log(this.getDataShape());
}
//UnitTest self
function util_ut() {
var d = getD()
d.ut();
}
//Test: C-s -> C-r -> C-Enter
//if you get [row,col] of the sheet named "data" -> success.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment