Skip to content

Instantly share code, notes, and snippets.

@rotimi-best
Last active August 19, 2019 10:07
Show Gist options
  • Save rotimi-best/b1cefa2282c40d6451a19469cd9c0239 to your computer and use it in GitHub Desktop.
Save rotimi-best/b1cefa2282c40d6451a19469cd9c0239 to your computer and use it in GitHub Desktop.
Convert your google spreadsheet data to json
// 1. Open your google spreadsheet
// 2. Tools > Script editor
// 3. Copy the code and paste in the editor.
// 4. Save the file and run it (you may need to give permissions)
function exportSheetAsJSON() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var numCols = rows.getNumColumns();
var values = rows.getValues();
var output = "";
output += "{\""+sheet.getName()+"\" : {\n";
var header = values[0];
for (var i = 1; i < numRows; i++) {
if (i > 1) output += " , \n";
var row = values[i];
output += "\""+row[0]+"\" : {";
for (var a = 1;a<numCols;a++){
if (a > 1) output += " , ";
output += "\""+header[a]+"\" : \""+row[a]+"\"";
}
output += "}";
//Logger.log(row);
}
output += "\n}}";
Logger.log(output);
DriveApp.createFile(sheet.getName()+".json", output, MimeType.PLAIN_TEXT);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment