Created
May 12, 2019 16:59
-
-
Save syakesaba/4e2b71105335a152cd70d3322778e69e to your computer and use it in GitHub Desktop.
GASでSpreadsheetを使う際にキャッシュアクセスをするスクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//"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