Last active
February 1, 2020 22:49
-
-
Save ttresslar/bc3d3cab28c7e0c02e13c2ffb57b4944 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
//Setting Global variables for the apps I'll be using | |
var SS = SpreadsheetApp.getActiveSheet() | |
var UI = SpreadsheetApp.getUi(); | |
//On open is a reserved function. It runs every time a user opens the sheet. | |
//I'm using it to make the menu | |
function onOpen() { | |
var UI = SpreadsheetApp.getUi(); | |
UI.createMenu('Data Archtects') | |
.addItem('Insert This data', 'makeItHot') | |
.addItem('Resize Cells', 'resetSizes') | |
.addToUi(); | |
} | |
//This opens the prompt to enter the table name | |
function showPrompt() { | |
var result = UI.prompt( | |
'Please enter the table name:', | |
UI.ButtonSet.OK_CANCEL); | |
var button = result.getSelectedButton(); | |
var text = result.getResponseText(); | |
if (button == UI.Button.OK) { | |
return text; | |
} else if (button == UI.Button.CANCEL) { | |
return "" | |
} else if (button == UI.Button.CLOSE) { | |
return ""; | |
} | |
} | |
//This is the main function. It digests the table an dturns it into a sql statement. | |
function makeItHot() { | |
var myText = showPrompt(); | |
if (myText === "") {return}; | |
var myData = SS.getActiveRange().getValues(); | |
var headers = myData[0]; | |
var data = myData.slice(1, myData.length) | |
var simplifiedData = []; | |
var headerString = "INSERT INTO "+ myText +" (" | |
headerString = headerString.concat(headers.join()); | |
headerString = headerString.concat(") \n VALUES \n"); | |
for (var i=0;i<data.length;i++){ | |
headerString = headerString.concat("("); | |
var line = data[i].map(stringDis) | |
headerString = headerString.concat(line); | |
headerString = headerString.concat(")") | |
if (i<data.length-1){ | |
headerString = headerString.concat(",\n"); | |
} | |
} | |
recordIt(headerString); | |
//the end of this function returns an alert that holds the insert statment | |
return UI.alert(headerString); | |
} | |
//this is a dumb little function that handles the different types of cells | |
//basically handles integers, dates, and strings. Also handles apostrophes | |
function stringDis(x) { | |
if (typeof x == 'number') { | |
return x | |
} else if (typeof x == 'object') { | |
return "'"+Utilities.formatDate(x, "EST", "yyyy-MM-dd")+"'"; | |
} else { | |
return "'"+x.replace(/\'/g,"\'\'")+"'"; | |
} | |
} | |
//this little guy makes a query history, so if I need the statment for anything later on, I can grab it. | |
function recordIt (query) { | |
var historySheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("History"); | |
historySheet.appendRow([new Date(), query]); | |
} | |
//sometimes I insert data and need to resize the cells, This undoes that | |
function resetSizes() { | |
var height = 21; | |
var width = 100; | |
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); | |
ss.setColumnWidths(1, ss.getLastColumn(), width); | |
ss.setRowHeights(1, ss.getLastRow(), height); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a quick script that takes your data and creates a SQL insert statement to input the data into a table that you specify.