0
votes

This is my first ever attempt with Apps Script and I'm trying to save a Google Spreadsheet as a csv file on Google Drive in a specific folder for archiving purposes. The "Checkout" sheet is one of several sheets in the Spreadsheet that contains a summary of the items that the client has requested. The sheet would include a "Confirm" button that, if clicked, should save the Checkout sheet only in the clients folder as a csv file. I've written the code below, however, I keep getting the following error "ReferenceError: "convertRangeToCsvFile_" is not defined.".

Could anyone please help with this?

function completeCheckout() {
 var coS = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Checkout");
 var clientFolder = DriveApp.getFolderById("14obb1o2gZFWMaSXQ7QF_2SemydGXIVLX");
 var clientName = "Client1";
 var TimeStamp  = Utilities.formatDate(new Date(), "GMT+2", "dd-MM-yyyy hh:mm:ss aaa");
 var fileName = clientName + TimeStamp + ".csv";
  var csvFile = convertRangeToCsvFile_(fileName,coS);

   clientFolder.createFile(fileName, csvFile);

}
2

2 Answers

0
votes

This code expects a function called "convertRangeToCsvFile_", which does not exist. This is why you receive the "is not defined" error.

You will need to create this function, or, assuming you are taking this line of code from an example somewhere, copy the relevant function from that example.

0
votes
function completeCheckout() {
  var ss=SpreadsheetApp.getActive()
  var sh=ss.getSheetByName("Checkout");
  var rg=sh.getDataRange();
  var vA=rg.getValues();
  var clientFolder = DriveApp.getFolderById("14obb1o2gZFWMaSXQ7QF_2SemydGXIVLX");
  var clientName = "Client1";
  var TimeStamp  = Utilities.formatDate(new Date(), "GMT+2", "dd-MM-yyyy hh:mm:ss aaa");
  var fileName = clientName + TimeStamp + ".csv";
  var csv='';
  vA.forEach(function(r,i){if(i>0){csv+='\n';}r.forEach(function(c,j){if(j>0){csv+=','};csv+=c.toString().replace(/(["'])/gi,'\\$1').replace(/[,]/gi,' ');});});
  clientFolder.createFile(fileName,csv)
}