Skip to content

Instantly share code, notes, and snippets.

@rafinskipg
Last active May 25, 2022 12:37
Show Gist options
  • Save rafinskipg/03ead6858c0c4626f813a3e71598b04b to your computer and use it in GitHub Desktop.
Save rafinskipg/03ead6858c0c4626f813a3e71598b04b to your computer and use it in GitHub Desktop.
Export Tags MakerDAO Gov Polls
/**
* @OnlyCurrentDoc
*/
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [
{name: "Export Tags", functionName: "exportSheet"},
{name: "Export Tag Definition", functionName: "exportTagDefinition"}
];
ss.addMenu("MakerDAO Governance", 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 exportTagDefinition() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('TagDefines');
var headersRange = sheet.getRange(1,1, sheet.getMaxRows(), sheet.getMaxColumns());
var headerValues = headersRange.getValues()[0];
var dataRange = sheet.getRange(2, 1, sheet.getMaxRows(), sheet.getMaxColumns());
var values = dataRange.getValues();
var objects = [];
for (var i = 0; i < values.length; ++i) {
const tagId = values[i][0]
if (isCellEmpty_(tagId)) {
continue;
} else {
var object = {}
for (var j = 0; j < headerValues.length; ++j) {
if (!isCellEmpty_(values[i][j])) {
object[headerValues[j]] = values[i][j]
}
}
objects.push(object);
}
}
var json = makeJSON_(objects);
displayText_(json);
}
function exportSheet(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('TagOutput');
var rowsData = getRowsData_(sheet);
var json = makeJSON_(rowsData);
displayText_(json);
}
function makeJSON_(object) {
var jsonString = JSON.stringify(object, null, 4);
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) {
var dataRange = sheet.getRange(2, 1, sheet.getMaxRows(), sheet.getMaxColumns());
var objects = getObjects_(dataRange.getValues());
var objectsById = {};
objects.forEach(function(object) {
objectsById[object.pollId] = object.tags;
});
return objectsById;
}
// 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) {
var objects = [];
for (var i = 0; i < data.length; ++i) {
var object = {};
const pollId = data[i][0]
if (isCellEmpty_(pollId)) {
continue;
} else {
object.pollId = pollId
object.tags = []
for (var j = 1; j < data[i].length; ++j) {
var tag = data[i][j];
if (isCellEmpty_(tag)) {
continue;
}
object.tags.push(tag);
}
objects.push(object);
}
}
return objects;
}
// 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';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment