Created
August 2, 2015 13:28
-
-
Save dommmel/b7e0f52a046b392c9c93 to your computer and use it in GitHub Desktop.
Google Apps Script to upload and import a CSV File into a Google Spreadsheet
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
// http://stackoverflow.com/questions/11273268/script-import-local-csv-in-google-spreadsheet | |
function onOpen() { | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
var csvMenuEntries = [{name: "Upload CSV file", functionName: "doGet"}]; | |
ss.addMenu("CSV", csvMenuEntries); | |
} | |
function doGet(e) { | |
var app = UiApp.createApplication().setTitle("Upload CSV to Sheet"); | |
var formContent = app.createVerticalPanel(); | |
formContent.add(app.createFileUpload().setName('thefile')); | |
formContent.add(app.createSubmitButton('Start Upload')); | |
var form = app.createFormPanel(); | |
form.add(formContent); | |
app.add(form); | |
SpreadsheetApp.getActiveSpreadsheet().show(app); | |
} | |
function doPost(e) { | |
// data returned is a blob for FileUpload widget | |
var fileBlob = e.parameter.thefile; | |
// parse the data to fill values, a two dimensional array of rows | |
// Assuming newlines separate rows and commas separate columns, then: | |
var values = [] | |
var rows = fileBlob.contents.split('\n'); | |
for(var r=0, max_r=rows.length; r<max_r; ++r) | |
values.push( rows[r].split(',') ); // rows must have the same number of columns | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
var sheet = ss.getActiveSheet(); | |
for (var i = 0; i < values.length; i++) { | |
sheet.getRange(i+1, 1, 1, values[i].length).setValues(new Array(values[i])); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Exception: UiApp has been deprecated. Please use HtmlService instead.
I get this error.