4

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.

Any help appreciated

Community
  • 1
  • 1
KMJO
  • 85
  • 1
  • 3
  • 13

1 Answers1

9

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);
AshClarke
  • 2,990
  • 5
  • 21
  • 27
  • This is very good. Special admiration to people who just remember things from the top of their head and don't need to test it, but for the record, I tested it and it worked exactly as expected. – delimiter Jul 09 '21 at 21:02