Skip to content

Instantly share code, notes, and snippets.

@daichan4649 daichan4649/code.gs
Created Feb 6, 2017

Embed
What would you like to do?
notify last report
var URL_BOOK = '[https://docs.google.com/spreadsheet/ccc?key=xxxxx]';
var SHEET_NAME = '[sheet name]';
var ROW_NUM_TITLE = 15; // タイトル行(jsonの'key'になる行)
function doGet(e) {
var json = createJson();
return JSON.stringify(json);
}
function createJson() {
var book = SpreadsheetApp.openByUrl(URL_BOOK);
var sheet = book.getSheetByName(SHEET_NAME);
return convertSheet2Json(sheet);
}
function convertSheet2Json(sheet) {
// title
var rowNum = ROW_NUM_TITLE;
var firstRange = sheet.getRange(rowNum, 1, rowNum, sheet.getLastColumn());
var firstRowValues = firstRange.getValues();
var titleColumns = firstRowValues[0];
// data(1行分)
var rowValues = [];
var rowIndex = sheet.getLastRow(); // 最終行
var colStartIndex = 1; // A
var rowNum = 1; // 1行分
var range = sheet.getRange(rowIndex, colStartIndex, rowNum, sheet.getLastColumn());
var values = range.getValues();
rowValues.push(values[0]);
// create json
var jsonArray = [];
for(var i=0; i<rowValues.length; i++) {
var line = rowValues[i];
var json = new Object();
for(var j=0; j<titleColumns.length; j++) {
json[titleColumns[j]] = line[j];
}
jsonArray.push(json);
}
return jsonArray;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.