Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export a Google Sheet to CSV on form submit

I would like to export a Google Sheet to CSV on form submit. I have searched but the only thing similar does not separate it into different columns once I have converted it: Export spreadsheet or CSV text file from Google Drive Forms using script?

It's a simple sheet with 8 columns only.

like image 791
KMJO Avatar asked Oct 17 '25 06:10

KMJO


1 Answers

Once you get the values of a spreadsheet range, you'll end up with a two dimensional array. So to convert it, you'll have to construct a string that joins array array cell elements with a comma and join the rows with a new-line ("\n");

Haven't tested this, but should be something like this.

var range = someSheet.getRange("A2:B" + someSheet.getLastRow());
var vals = range.getValues();
//let's pretend vals is as below
var vals = [["A2","B2"], ["A3", "B3"]];
var csvString = vals.join("\n");
DriveApp.createFile("mycsv.csv", csvString);
like image 116
AshClarke Avatar answered Oct 20 '25 02:10

AshClarke