Posts tagged with email

I've been working on this google ads script to email a csv to me. It queries the ad words api for data, writes it to a spreadsheet, reads it from the spreadsheet, iterates through the rows to create a csv string, creates a blob and sends the email.

The reason it writes/reads to a spreadsheet is to get the data in the correct format. I'm not sure there is a better way.

The problem is that it will not create the blob. It fails with this error:

Exception: Unexpected error while getting the method or property newBlob on object Utilities. (line 53) 

I've looked online and can not find much on this error. I've tried removing arguments on the newBlob statement, sending the raw csv string, etc. Nothing seems to work and I can't figure out why creating the blob fails.

const SS_ID = '1_super_secret_id_Q'; const SS_NAME = 'GCLID Report'; const SHEET_NAME = 'DURING YESTERDAY' const EMAIL_TO = 'c_super_secret_email_g' const MILLIS_PER_DAY = 1000 * 60 * 60 * 24; function collect_report() {   return AdsApp.report(     'SELECT                ' +     '  click_view.gclid,   ' +     '  segments.date,      ' +     '  customer.id,        ' +     '  ad_group.id,        ' +     '  campaign.id         ' +     'FROM click_view       ' +     'WHERE segments.date DURING YESTERDAY'   ); } function calculate_yesterday() {   var now = new Date();   var yesterday = new Date(now.getTime() - MILLIS_PER_DAY);   var timeZone = AdsApp.currentAccount().getTimeZone();   var yesterday_str = Utilities.formatDate(yesterday, timeZone, 'yyyy-MM-dd');   return yesterday_str; } function convert_sheet_to_csv(sheet) {   var csv_data = '';      // This represents ALL the data.   var range = sheet.getDataRange();   var values = range.getValues();   // This logs the spreadsheet in CSV format.   for (let i = 0; i < values.length; i++) {     csv_data += (values[i].join(',')) + '\r\n';   } } function main() {   var report = collect_report();   var spreadsheet = SpreadsheetApp.openById(SS_ID);   var sheet = spreadsheet.getSheetByName(SHEET_NAME);      sheet.clearContents();   report.exportToSheet(sheet);   var mime = 'text/plain'   var yesterday_str = calculate_yesterday();   var filename = 'gclid_report.csv';   var csv_data = convert_sheet_to_csv(sheet);   var blob = Utilities.newBlob(csv_data, mime, filename);   var email_subject = 'Your Google Ads Report Is Ready: GCLID Report';   var email_body = 'Attached is the ' + filename;   MailApp.sendEmail(     EMAIL_TO,     email_subject,     email_body,     {         name: 'Automatic Emailer Script',         attachments: [blob]     }   ); } main();