About Google Apps Script MailApp.sendEmail Errors

Asked 1 years ago, Updated 1 years ago, 40 views

function attachmentsToCsvFile(){
  varsheet=SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
  // You could use now Date(); on its own but it will not look nice.
  vardate=Utilities.formatDate(newDate(), "GMT-4", "ddMMyyyy")
  variableendDate=date


  varcsvFileName=date+'.csv';
  vars = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('sheet 1');
  varmaxColumn=ss.getLastColumn();
  varmaxRow=ss.getLastRow();
  vardata=ss.getRange(1,1,maxRow,maxColumn).getValues();
    if(data.length>1){
  varcsv="";
  for (varrow=0; row<data.length;row++){
    for (varcol=0; col<data[row].length;col++){
      if(data[row][col].toString().indexOf(",")!=-1){
        data[row][col]="\"+data[row][col]+"\";
      }
    }

    if(row<data.length-1){
      csv+=data[row].join(", ")+"\r\n";
    }
    else{
      csv+=data[row];
    }
  }
  csvFile=csv;
}

  var attachments = [{fileName:csvFileName, mimeType:'text/comma-separated- 
 values', content:csvFile}];
  try{
  MailApp.sendEmail("[email protected]", "CSV file", "CSV file", 
{attachments:attachments});
}catch(error) {throw newError("More meetingful error.");
}
}

I am trying to send the contents of Google SpreadSheet as an attachment to a CSV file with this code.
There is an error in MailApp.sendemail, but I don't know what kind of error it is, so I don't know the specifications of the action.
I looked up the code to get a few errors, but none of them worked.

Also, if it is the same code and a different sheet, it will work fine.
Therefore, it may be possible that there is something in the sheet that CSV cannot do, but the data is alphanumeric, hyphen, and period only.

Thank you for your cooperation.

Add

Running Transcript

[18-09-1101:37:34:638 HKT] Running
[18-09-1101:37:34:654HKT] SpreadsheetApp.getActiveSpreadsheet() [0 seconds]
[18-09-1101:37:34:762 HKT] Spreadsheet.getSheetByName ([Sheet 1]) [0.108 seconds]
[18-09-1101:37:34:763 HKT] Utilities.formatDate ([Mon Sep 10 10:37:34 PDT 2018, GMT-4, ddMMyyyy]) [0 seconds]
[18-09-1101:37:34:764 HKT] SpreadsheetApp.getActiveSpreadsheet() [0 seconds]
[18-09-1101:37:34:765 HKT] Spreadsheet.getSheetByName ([Sheet 1]) [0 seconds]
[18-09-1101:37:35:559HKT] Sheet.getLastColumn() [0.794 seconds]
[18-09-1101:37:35:795 HKT] Sheet.getLastRow() [0.236 seconds]
[18-09-1101:37:35:796 HKT] Sheet.getRange ([1,1,3146,9]) [0 seconds]
[18-09-1101:37:36:516 HKT] Range.getValues() [0.719 seconds]
[18-09-1101:37:38:403 HKT] MailApp.sendEmail([[email protected],CSV file,CSV file,{ attachments=[{fileName=10092018NYSE.csv,mimeType=text/comma-separated-values, content= Symbol, Description, Time, symbol, power, police, police, power,
A, Agilent Technology}]]...) [0.133 seconds]
[18-09-1101:37:38:413 HKT] Run successfully (total runtime 3.752 seconds)

It is supposed to be running without any errors, but
No mail has actually been sent.

google-apps-script

2022-09-30 18:03

2 Answers

Sorry for the trouble.

Because the script runs and even sends mail without any problems on the other sheets. Thinking that the data contains some problematic string,
I looked for it in a roundabout way.
I don't know why at all, but the string "SBI" seems to be causing the problem.
It was executed like any other sheet by deleting a row with that string.
It can now be rewritten to "SB" or "SBIA" strings without any problems.
I think it's some kind of bug.


2022-09-30 18:03

https://developers.google.com/apps-script/reference/mail/mail-app

If you look at the command, it says Void. What is this???


2022-09-30 18:03

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.