Created
January 27, 2015 15:20
-
-
Save hashrock/1324269b6ba1ff7a5e72 to your computer and use it in GitHub Desktop.
This file contains 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
//Original : https://gist.github.com/daichan4649/8877801#file-convertsheet2jsontext-gs | |
//2015/01/28 add SetUp method | |
function doGet(e) { | |
var book = SpreadsheetApp.openById(PropertiesService.getScriptProperties().getProperty('active')); | |
var sheet = book.getSheets[0]; | |
var json = convertSheet2JsonText(sheet); | |
return ContentService.createTextOutput(JSON.stringify(json)).setMimeType(ContentService.MimeType.JSON); | |
} | |
function convertSheet2JsonText(sheet) { | |
// first line(title) | |
var colStartIndex = 1; | |
var rowNum = 1; | |
var firstRange = sheet.getRange(1, 1, 1, sheet.getLastColumn()); | |
var firstRowValues = firstRange.getValues(); | |
var titleColumns = firstRowValues[0]; | |
// after the second line(data) | |
var lastRow = sheet.getLastRow(); | |
var rowValues = []; | |
for(var rowIndex=2; rowIndex<=lastRow; rowIndex++) { | |
var colStartIndex = 1; | |
var rowNum = 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; | |
} | |
function setUp() { | |
PropertiesService.getScriptProperties().setProperty('active', SpreadsheetApp.getActiveSpreadsheet().getId()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment