简体   繁体   中英

Missing variable name while trying to send sheet row data to GmailApp; Javascript/Google App script

Still on a learning curve here. I am trying to get data from a google sheet and send the data Gmail app but getting Missing variable name error. This is what I have tried for the last 2 hours and I will appreciate help in structuring the code to work.

function emailTheLastRow(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();  
   var range = sheet.getRange("AO2:AO"+sheet.getLastRow()).getValues();
  var searchString = "1";

  for (var i = 0; i<range.length; i++) {
  if(range[i][0] == searchString) {
   var lastRow = sheet.getRange(2+i,1,1,41).getValues();
     var data = {    
       'email':lastRow[0][1],
       'project':lastRow[0][2],
       'client':lastRow[0][3],
       'sdate':lastRow[0][4],
       'edate':lastRow[0][5],
       'loe':lastRow[0][7],
     };

      GmailApp.sendEmail("test@gmail.com", "Project", "A new project has been created with the following details: " + data());

    }
  }
}

You're trying to concatenate a string with a JSON, which is an object and not a string. Use stringify function [1] to convert the object to a string. Add this line after you declare data variable:

 data = JSON.stringify(data);

[1] https://www.w3schools.com/js/js_json_stringify.asp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM