Google Apps Script - convert to CSV

2019-01-29 03:10发布

问题:

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

回答1:

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);