Skip to content

Instantly share code, notes, and snippets.

@naosim
Last active July 17, 2019 01:26
Show Gist options
  • Save naosim/4c3b3be951c359a177dd8f2a100bc503 to your computer and use it in GitHub Desktop.
Save naosim/4c3b3be951c359a177dd8f2a100bc503 to your computer and use it in GitHub Desktop.
【GAS】シートをDBのように扱う。オブジェクト版
function SheetDb(spreadSheet) {
/**
* レコードの全取得
*/
function findAll(name /* シート名 */) {
var sheet = spreadSheet.getSheetByName(name);
var table = sheet.getDataRange().getValues()
return JSON.parse(JSON.stringify(table));
}
/**
* データの挿入
*/
function insert(
name, /* シート名 */
values /* 1行分の配列 */
) {
var sheet = spreadSheet.getSheetByName(name);
sheet.appendRow(values)
}
/**
* 行の更新
*/
function update(
name, /* シート名 */
rowIndex,/* zero origin */
values /* 1行分の配列 */
) {
var sheet = spreadSheet.getSheetByName(name);
sheet.getRange(rowIndex + 1, 1, 1, values.length).setValues([values])
}
/**
* 行の削除
*/
function remove(
name, /* シート名 */
rowIndexes /* zero origin */
) {
// 降順に並べる
rowIndexes = rowIndexes.sort(function(a, b) { return b - a });
var sheet = spreadSheet.getSheetByName(name);
rowIndexes.forEach(function(i) {
sheet.deleteRow(i + 1)
})
}
return {
findAll: findAll,
insert: insert,
update: update,
remove: remove
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment