Skip to content

Instantly share code, notes, and snippets.

@pamelafox
Last active September 15, 2024 09:28
Show Gist options
  • Save pamelafox/1878143 to your computer and use it in GitHub Desktop.
Save pamelafox/1878143 to your computer and use it in GitHub Desktop.
// Includes functions for exporting active sheet or all sheets as JSON object (also Python object syntax compatible).
// Tweak the makePrettyJSON_ function to customize what kind of JSON to export.
var FORMAT_ONELINE = 'One-line';
var FORMAT_MULTILINE = 'Multi-line';
var FORMAT_PRETTY = 'Pretty';
var LANGUAGE_JS = 'JavaScript';
var LANGUAGE_PYTHON = 'Python';
var STRUCTURE_LIST = 'List';
var STRUCTURE_HASH = 'Hash (keyed by "id" column)';
/* Defaults for this particular spreadsheet, change as desired */
var DEFAULT_FORMAT = FORMAT_PRETTY;
var DEFAULT_LANGUAGE = LANGUAGE_JS;
var DEFAULT_STRUCTURE = STRUCTURE_LIST;
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [
{name: "Export JSON for this sheet", functionName: "exportSheet"},
{name: "Export JSON for all sheets", functionName: "exportAllSheets"}
];
ss.addMenu("Export JSON", menuEntries);
}
function makeLabel(app, text, id) {
var lb = app.createLabel(text);
if (id) lb.setId(id);
return lb;
}
function makeListBox(app, name, items) {
var listBox = app.createListBox().setId(name).setName(name);
listBox.setVisibleItemCount(1);
var cache = CacheService.getPublicCache();
var selectedValue = cache.get(name);
Logger.log(selectedValue);
for (var i = 0; i < items.length; i++) {
listBox.addItem(items[i]);
if (items[1] == selectedValue) {
listBox.setSelectedIndex(i);
}
}
return listBox;
}
function makeButton(app, parent, name, callback) {
var button = app.createButton(name);
app.add(button);
var handler = app.createServerClickHandler(callback).addCallbackElement(parent);;
button.addClickHandler(handler);
return button;
}
function makeTextBox(app, name) {
var textArea = app.createTextArea().setWidth('100%').setHeight('200px').setId(name).setName(name);
return textArea;
}
function exportAllSheets(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var sheetsData = {};
for (var i = 0; i < sheets.length; i++) {
var sheet = sheets[i];
var rowsData = getRowsData_(sheet, getExportOptions(e));
var sheetName = sheet.getName();
sheetsData[sheetName] = rowsData;
}
var json = makeJSON_(sheetsData, getExportOptions(e));
displayText_(json);
}
function exportSheet(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var rowsData = getRowsData_(sheet, getExportOptions(e));
var json = makeJSON_(rowsData, getExportOptions(e));
displayText_(json);
}
function getExportOptions(e) {
var options = {};
options.language = e && e.parameter.language || DEFAULT_LANGUAGE;
options.format = e && e.parameter.format || DEFAULT_FORMAT;
options.structure = e && e.parameter.structure || DEFAULT_STRUCTURE;
var cache = CacheService.getPublicCache();
cache.put('language', options.language);
cache.put('format', options.format);
cache.put('structure', options.structure);
Logger.log(options);
return options;
}
function makeJSON_(object, options) {
if (options.format == FORMAT_PRETTY) {
var jsonString = JSON.stringify(object, null, 4);
} else if (options.format == FORMAT_MULTILINE) {
var jsonString = Utilities.jsonStringify(object);
jsonString = jsonString.replace(/},/gi, '},\n');
jsonString = prettyJSON.replace(/":\[{"/gi, '":\n[{"');
jsonString = prettyJSON.replace(/}\],/gi, '}],\n');
} else {
var jsonString = Utilities.jsonStringify(object);
}
if (options.language == LANGUAGE_PYTHON) {
// add unicode markers
jsonString = jsonString.replace(/"([a-zA-Z]*)":\s+"/gi, '"$1": u"');
}
return jsonString;
}
function displayText_(text) {
var output = HtmlService.createHtmlOutput("<textarea style='width:100%;' rows='20'>" + text + "</textarea>");
output.setWidth(400)
output.setHeight(300);
SpreadsheetApp.getUi()
.showModalDialog(output, 'Exported JSON');
}
// getRowsData iterates row by row in the input range and returns an array of objects.
// Each object contains all the data for a given row, indexed by its normalized column name.
// Arguments:
// - sheet: the sheet object that contains the data to be processed
// - range: the exact range of cells where the data is stored
// - columnHeadersRowIndex: specifies the row number where the column names are stored.
// This argument is optional and it defaults to the row immediately above range;
// Returns an Array of objects.
function getRowsData_(sheet, options) {
var headersRange = sheet.getRange(1, 1, sheet.getFrozenRows(), sheet.getMaxColumns());
var headers = headersRange.getValues()[0];
var dataRange = sheet.getRange(sheet.getFrozenRows()+1, 1, sheet.getMaxRows(), sheet.getMaxColumns());
var objects = getObjects_(dataRange.getValues(), normalizeHeaders_(headers));
if (options.structure == STRUCTURE_HASH) {
var objectsById = {};
objects.forEach(function(object) {
objectsById[object.id] = object;
});
return objectsById;
} else {
return objects;
}
}
// getColumnsData iterates column by column in the input range and returns an array of objects.
// Each object contains all the data for a given column, indexed by its normalized row name.
// Arguments:
// - sheet: the sheet object that contains the data to be processed
// - range: the exact range of cells where the data is stored
// - rowHeadersColumnIndex: specifies the column number where the row names are stored.
// This argument is optional and it defaults to the column immediately left of the range;
// Returns an Array of objects.
function getColumnsData_(sheet, range, rowHeadersColumnIndex) {
rowHeadersColumnIndex = rowHeadersColumnIndex || range.getColumnIndex() - 1;
var headersTmp = sheet.getRange(range.getRow(), rowHeadersColumnIndex, range.getNumRows(), 1).getValues();
var headers = normalizeHeaders_(arrayTranspose_(headersTmp)[0]);
return getObjects(arrayTranspose_(range.getValues()), headers);
}
// For every row of data in data, generates an object that contains the data. Names of
// object fields are defined in keys.
// Arguments:
// - data: JavaScript 2d array
// - keys: Array of Strings that define the property names for the objects to create
function getObjects_(data, keys) {
var objects = [];
for (var i = 0; i < data.length; ++i) {
var object = {};
var hasData = false;
for (var j = 0; j < data[i].length; ++j) {
var cellData = data[i][j];
if (isCellEmpty_(cellData)) {
continue;
}
object[keys[j]] = cellData;
hasData = true;
}
if (hasData) {
objects.push(object);
}
}
return objects;
}
// Returns an Array of normalized Strings.
// Arguments:
// - headers: Array of Strings to normalize
function normalizeHeaders_(headers) {
var keys = [];
for (var i = 0; i < headers.length; ++i) {
var key = normalizeHeader_(headers[i]);
if (key.length > 0) {
keys.push(key);
}
}
return keys;
}
// Normalizes a string, by removing all alphanumeric characters and using mixed case
// to separate words. The output will always start with a lower case letter.
// This function is designed to produce JavaScript object property names.
// Arguments:
// - header: string to normalize
// Examples:
// "First Name" -> "firstName"
// "Market Cap (millions) -> "marketCapMillions
// "1 number at the beginning is ignored" -> "numberAtTheBeginningIsIgnored"
function normalizeHeader_(header) {
var key = "";
var upperCase = false;
for (var i = 0; i < header.length; ++i) {
var letter = header[i];
if (letter == " " && key.length > 0) {
upperCase = true;
continue;
}
if (!isAlnum_(letter)) {
continue;
}
if (key.length == 0 && isDigit_(letter)) {
continue; // first character must be a letter
}
if (upperCase) {
upperCase = false;
key += letter.toUpperCase();
} else {
key += letter.toLowerCase();
}
}
return key;
}
// Returns true if the cell where cellData was read from is empty.
// Arguments:
// - cellData: string
function isCellEmpty_(cellData) {
return typeof(cellData) == "string" && cellData == "";
}
// Returns true if the character char is alphabetical, false otherwise.
function isAlnum_(char) {
return char >= 'A' && char <= 'Z' ||
char >= 'a' && char <= 'z' ||
isDigit_(char);
}
// Returns true if the character char is a digit, false otherwise.
function isDigit_(char) {
return char >= '0' && char <= '9';
}
// Given a JavaScript 2d Array, this function returns the transposed table.
// Arguments:
// - data: JavaScript 2d Array
// Returns a JavaScript 2d Array
// Example: arrayTranspose([[1,2,3],[4,5,6]]) returns [[1,4],[2,5],[3,6]].
function arrayTranspose_(data) {
if (data.length == 0 || data[0].length == 0) {
return null;
}
var ret = [];
for (var i = 0; i < data[0].length; ++i) {
ret.push([]);
}
for (var i = 0; i < data.length; ++i) {
for (var j = 0; j < data[i].length; ++j) {
ret[j][i] = data[i][j];
}
}
return ret;
}
@EFox2413
Copy link

EFox2413 commented Jun 2, 2021

Not sure who else this might help but I was getting a weird error with a URL being cut off when exporting to JSON. I was able to fix it by updating the displayText_ function to use a <pre> tag instead of <textarea>

@bulleet
Copy link

bulleet commented Sep 27, 2021

I use a lot of imbricated data, so I modified a bit the code for it to be possible without directly typing JSON inside a cell.
I use the column title as the chain of property names, separated by a dot.

It ends up like that
Capture d’écran de 2021-04-10 12-02-09

I've done that quickly, it may broke in some cases, I don't know.

To do so

Replace line 184 with
setObjectData_(object, keys[j], cellData);

Replace line 226 with
if (letter !== "." && !isAlnum_(letter)) {

Add this function somewhere

// For every key in the keys array
// recursively fill the object with data
// Arguments:
//   - object: JavaScript object to fill
//   - keys: Array (or '.' separated String) that deifine the imbricated properties names for the object to fill
//   - data: value that should be put
function setObjectData_(object, keys, data) {
  if (!Array.isArray(keys)) {
    keys = keys.split(".");
  }
  if (keys.length == 1) {
    object[keys[0]] = data;
  } else {
    if (!object.hasOwnProperty(keys[0])) {
      if (!isNaN(keys[1]))
        object[keys[0]] = [];
      else
        object[keys[0]] = {};
    } else {
      // if the first property is a number it'll create an array
      // but if another property is a string it shall be converted to an object
      if (Array.isArray(object[keys[0]]) && isNaN(keys[1]))
        object[keys[0]] = Object.assign({}, object[keys[0]]);
    }
    setObjectData_(object[keys[0]], keys.slice(1), data);
  }
}

Hi, what about creating a dictionary like this:
{ "name": { "key1": "value1", "key2": "value2", } }
when 'name', 'key' and 'value' are contained in 3 different columns.
I will be eternally grateful for your help.

@florentdescroix
Copy link

@bulleet I made a fork with many more changes I've done for my personal use, that maybe useful.

If I understood clearly what you want, I think you shoud do something like that :
image
Which will generate this JSON

{
    "toto": {
        "tutu": "foo",
        "tata": "bar"
    },
    "fufu": {
        "fifi": "doe",
        "fafa": "jo"
    }
}

@bulleet
Copy link

bulleet commented Oct 7, 2021

@bulleet I made a fork with many more changes I've done for my personal use, that maybe useful.

If I understood clearly what you want, I think you shoud do something like that : image Which will generate this JSON

{
    "toto": {
        "tutu": "foo",
        "tata": "bar"
    },
    "fufu": {
        "fifi": "doe",
        "fafa": "jo"
    }
}

Yes, but instead of 'toto.tutu', I'd prefer to store 'toto' and 'tutu' in two separate columns, but this will also do work.

@c-mille
Copy link

c-mille commented Oct 29, 2021

Thanks a lot <3

@alex-drocks
Copy link

Thanks

@swrh
Copy link

swrh commented Apr 15, 2022

This script doesn't handle HTML code correctly. This is what I did in the displayText_ function to fix it.

const escapeHtml_ = (unsafe) => {
  return unsafe
    .replaceAll('&', '&amp;')
    .replaceAll('<', '&lt;')
    .replaceAll('>', '&gt;')
    .replaceAll('"', '&quot;')
    .replaceAll("'", '&#039;')
}

const displayText_ = (text) => {
  const output = HtmlService.createHtmlOutput('<pre>' + escapeHtml_(text) + '</pre>')
  SpreadsheetApp.getUi()
      .showModalDialog(output, 'Exported JSON')
}

@Alaric-Malikov
Copy link

I have an idea that will make most of the coders using this bit alot happier (myself included). When your code runs into a cell with no data in it, your program does not include that key in the JSON object. Instead, I advise including this key, but setting it's value as a JSON "null" value, so when the programmer's code refers to this key in any given object, an error will not be raised for a non-existent key. What do you think?

@florentdescroix
Copy link

I have an idea that will make most of the coders using this bit alot happier (myself included). When your code runs into a cell with no data in it, your program does not include that key in the JSON object. Instead, I advise including this key, but setting it's value as a JSON "null" value, so when the programmer's code refers to this key in any given object, an error will not be raised for a non-existent key. What do you think?

Check out this fork , you'll just have to set the first variable the way you want ;)

@Alaric-Malikov
Copy link

Alaric-Malikov commented May 16, 2022

I am not sure as to what you mean by "the way you want". Keep in mind that my JavaScript is not the best, so dumbing it down for me may be a bit necessary. I do have a few years of programming under my belt tho (between Python, GDScript, C++, and a few others), so you dont have to go to far in the dumbing :P

@Alaric-Malikov
Copy link

you know what, I think I understand. You mean that I should set the very first var to be whatever value I want the empty cells to be (I.E null). I tested it, and it works as I hoped. I have but one more question:

I am going to use this code to export JSON of item objects for a game. What I want is this:

[ "10001": { "Name": "Spiked Battle Helm", "Category": "Apparel", "Type": "Helmet", "EquipmentSlot": "Head", "Attack": null, "Defense": 20, "Block": null, "Luck": null, "HealthRestore": null, "ManaRestore": null, "Stackable": false, "Sex": "B", "Origin": "Endal", "Value": 90, "Enchantable": true, "Craftable": false, "Shifting": true, "ShifterVal": 3, "ItemRarity": null, "AtkSpeed": null, "GreatAtk": null },
and what I get from your code is:
[ { "ID": 10001, "Name": "Spiked Battle Helm", "Category": "Apparel", "Type": "Helmet", "EquipmentSlot": "Head", "Attack": null, "Defense": 20, "Block": null, "Luck": null, "HealthRestore": null, "ManaRestore": null, "Stackable": false, "Sex": "B", "Origin": "Endal", "Value": 90, "Enchantable": true, "Craftable": false, "Shifting": true, "ShifterVal": 3, "ItemRarity": null, "AtkSpeed": null, "GreatAtk": null },

Is there a way I can do this by altering your code (or you making another fork like you did, though I thought you would want me to do the code myself)? My JavaScript, as mentioned before, is not the best, so I need a little push as to where I should be looking to make this happen. Thanks for your help!

@florentdescroix
Copy link

@Alaric-Malikov so you found by yourself and did the exact right thing !
About your second issue, I think it is simply because you named your column 'ID' instead of 'id'... I could have anticipated that but well, I didn't ah ah!

(for the future if you want to talk about my code, please comment over there, so this feed stays on its subject)

@Francof08
Copy link

Hi, Can someone know how to do a couple changes. I need to romove the first and last brackets and include them in after "listaDetalle" just before the ones already there

NOW
[
{
"usuario": "xxxx",
"password": "yyyy",
"ordenDeCompra": {
"codigo": "OC00013",
"sitio": "CENTRAL",
"proveedor": "ASN0001",
"tipo": "NAC",
"descripcion": "10001",
"listaDetalle": {
"sku": "01020",
"unidadesEnviadas": "90",
"campo01": "AABBCC",
"campo02": "AABBCC"
}
}
}
]

EXPECTED

{
    "usuario": "xxxx",
    "password": "yyyy",
    "ordenDeCompra": {
        "codigo": "OC00013",
        "sitio": "CENTRAL",
        "proveedor": "ASN0001",
        "tipo": "NAC",
        "descripcion": "10001",
        "listaDetalle": [{
            "sku": "01020",
            "unidadesEnviadas": "90",
            "campo01": "AABBCC",
            "campo02": "AABBCC"
        }]
    }
}

@florentdescroix
Copy link

florentdescroix commented Jun 3, 2022

@Francof08 your structure is way to imbricated, it is really hard to immagine it fiting in a two dimentional array that is a SpreadSheet.
So I think you'll have to do some manual workaround there :/

Otherwise, you can name your columns the following way : "ordenDeCompra.listaDetalle.0.sku" (and so on for "unidadesEnviadas" and "campoXX") then increasing the "0" for each array entry.
It should word, but depending on the length of "listaDetalle" you may have tons of columns.

@lim1105
Copy link

lim1105 commented Sep 30, 2022

hi @pamelafox
wonder how you learn to create this script or where you get the resource to study on this?
would be nice if you okay to share some insights

@florentdescroix
Copy link

@lim1105 check out this doc : https://developers.google.com/sheets/api
(passing by Google Script allows to skip the whole authentification process)

@lim1105
Copy link

lim1105 commented Oct 13, 2022

@pamelafox thanks for the info!

@Isaac-Tait
Copy link

I have tried this on two separate spreadsheets and it isn't working... https://thenewstack.io/how-to-convert-google-spreadsheet-to-json-formatted-text/ took the OG post and reworked it a bit so it's more current. Anyone else figure out how to get this to work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment