Script import local CSV into Google Spreadsheet

How to import a CSV file from the local hard drive into the Google Spreadsheet document that I have? (I want to replicate the command File → Import via script)

+4
source share
2 answers

This seems like a wrong answer with a DocList, but instead of using a DocsList, you can get the data directly from the blob, analyze it, and then import it into a spreadsheet. I only summarize:

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); // return app; SpreadsheetApp.getActiveSpreadsheet().show(app);// 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 // Using active sheet here, but you can pull up a sheet in several other ways as well SpreadsheetApp.getActiveSheet() .getRange( 1, 1, values.length, values[0].length ) .setValues(values); } 
+5
source

We published a tutorial that shows how to import CSV into a spreadsheet.

+1
source

All Articles