Google Apps Script - Convert row data to dictionary
Converting a row data of a google spreadsheet to a dictionary is quite useful. Here is the snippet I wrote this morning:
With the dictionary format, you can access column data of that row easily, for example:
Logger.log(myDictData["username"]);
Pretty neat huh? \m/
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
function rowToDict(sheet, rownumber) { | |
var columns = sheet.getRange(1,1,1, sheet.getMaxColumns()).getValues()[0]; | |
var data = sheet.getDataRange().getValues()[rownumber-1]; | |
var dict_data = {}; | |
for (var keys in columns) { | |
var key = columns[keys]; | |
dict_data[key] = data[keys]; | |
} | |
return dict_data; | |
} |
With the dictionary format, you can access column data of that row easily, for example:
Logger.log(myDictData["username"]);
Pretty neat huh? \m/