Sending sms from Google sheets, error: no recipient - google-apps-script

First time using script this is what I have after a couple hours.
I'm setting up a daily text blast which in testing works but I get this error Exception: Failed to send email: no recipient
at sendtext(Code:8:9)
Here's my sheet:
https://i.stack.imgur.com/RS5c2.png
And my code:
function sendtext() {
var sh=SpreadsheetApp.getActive().getSheetByName("Word-a-Day");
var lrow=sh.getLastRow()
for(var i=2;i<=lrow;i++) {
MailApp.sendEmail(sh.getRange(i,2).getValue(), "Khmer Word-A-Day", sh.getRange(2, 3).getValue())
}
}
The test texts goes through but I get the an error every time. Is this because of using the sms gateway?

Try this:
function sendtext() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName("Word-a-Day");
var lrow=sh.getLastRow();
var rg=sh.getRange(2,2,sh.getLastRow()-1,2);
var vs=rg.getValues();
var q=MailApp.getRemainingDailyQuota();
for(var i=0;i<vs.length;i++) {
MailApp.sendEmail(vs[i][0], "Khmer Word-A-Day", vs[i][1]);
//Utilities.sleep(5000);//Perhaps you need a delay here.
}
}

Found random space in a cell in the text gateway column so it was throwing an error. Thanks for the help.

Related

Clear, copy and paste from one file to another using google script

I have this script running from one source file to several other files where I use the data. The script has always tended to work fine but recently started getting the error
Exception: Service Spreadsheets timed out while accessing document with id.
I only get the error when the script is running for a particular destination file, as opposed to others. It also happens that this destination file is much larger than the others (maybe the problem). Anyway, the script is below and wondering if anyone has any alternative suggestions or feedback.
function Function_name() {
var ss=SpreadsheetApp.getActiveSpreadsheet()
var sourcesheet=ss.getSheetByName("RAWFINEX")
var data=sourcesheet.getDataRange().getValues()
var destinationfile=SpreadsheetApp.openById("Sheet ID")
var destinationsheet=destinationfile.getSheetByName("RAWFINEX")
var destinationlr=lastUsableRow(destinationsheet)
var destinationlc=destinationsheet.getLastColumn
var existingdata=destinationsheet.getRange(1,1,destinationlr,5).clearContent
var destinationrange=destinationsheet.getRange(1,1,data.length,data[0].length)
var pastedata=destinationrange.setValues(data)
}
Copy Data And Log
function CopyDataAndLog() {
var log = DriveApp.getFileById("logid");
var ss=SpreadsheetApp.getActive();
var ssh=ss.getSheetByName("RAWFINEX");
var svs=ssh.getDataRange().getValues();
var ss=SpreadsheetApp.openById("Sheet ID")
var fi=DriveApp.getFileById(ss.getId())
log.setContent(log.getBlob().getDataAsString() + `${fi.getSize()},${file.getName()},${file.getId()},${file.getType()}${'\r\n'}}`;
var tsh=ss.getSheetByName("RAWFINEX")
var tlr=lastUsableRow(tsh)
tsh.getRange(1,1,tlr,5).clearContent();
tsh.getRange(1,1,svs.length,svs[0].length).trg.setValues(svs);
}

Count Number of Emails in Inbox by Sender [duplicate]

This question already has an answer here:
Count number of Gmail emails per sender
(1 answer)
Closed 2 years ago.
Referencing another user's question, I've been attempting to obtain the number of emails for each email address, from within my Gmail Inbox. I was sure to enable to Gmail API in Google Developer Console, as well as in the Google Apps Script. However, upon running the script, I receive an error which states
"Exception: Invalid argument: value (line 22, file "Code")".
I've been searching Google for some answers, but my lack of understanding has hindered my progress.
Below is the full code, excluding my email address. Any suggestions are greatly appreciated.
function sender_list_paged(token) {
var token=token||null;
var query="in:inbox";
var sender_array=[];
var uA=[]
var cObj={};
do{
var result=Gmail.Users.Messages.list("MY-EMAIL#gmail.com", {maxResults:10,pageToken:token,q:query});
var list=result;
Logger.log(list);
for(var i=0;i<list.messages.length;i++) {
var sender=GmailApp.getMessageById(list.messages[i].id).getFrom();
if(uA.indexOf(sender)==-1) {
uA.push(sender);
sender_array.push([sender]);
cObj[sender]=1;
}else{
cObj[sender]+=1;
}
}
token=list.nextPageToken;
PropertiesService.getUserProperties().setProperty("lastpagetoken", token);
}
while(token);
sender_array.forEach(function(r){
r.splice(1,0,cObj[r[0]]);
});
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet()
sh.clear();
sh.appendRow(['Email Address','Count']);
sh.getRange(2, 1,sender_array.length,2).setValues(sender_array).sort({column:1,ascending:true});
}
I see the post you are referring to has a workable solution that runs:
function sender_list() {
var inbox_threads=GmailApp.search('in:anywhere');
var sender_array=[];
var uA=[];
var cObj={};
for(var i=0;i<inbox_threads.length;i++) {
var message=inbox_threads[i].getMessages();
for(var x=0;x<message.length; x++) {
var sender=message[x].getFrom();
if(uA.indexOf(sender)==-1) {
uA.push(sender);
sender_array.push([sender]);
cObj[sender]=1;
}else{
cObj[sender]+=1;
}
}
}
sender_array.forEach(function(r){
r.splice(1,0,cObj[r[0]]);
});
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet()
sh.clear();
sh.appendRow(['Email Address','Count']);
sh.getRange(2, 1,sender_array.length,2).setValues(sender_array).sort({column:1,ascending:true});
}
Desclaimer:
The code used in this post comes from here:
Count number of Gmail emails per sender

Using the RelativeDate Enum

I'm trying to run a check that would send an email automatically if the due date is tomorrow on a piece of equipment. The problem is that I'm not sure if I can possibly use this -> https://developers.google.com/apps-script/reference/spreadsheet/relative-date?hl=ru To run the check.
This is what I have so far, hopefully someone can help me here, I'm still fairly new to app script, and am honestly not the best coder in general. Combine that with my inability to find good examples for app script, and my problems are fairly compounded.
function emailForLaptops() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName('Main');
var tomorrow = new Date(TOMORROW);
var active = sheet1.getActiveRange().getValues(); // gets the active highlighted data
var data = sheet1.getRange("C3:D21").getValues();
data.forEach(function(row) {
if (row[1] == tomorrow){
MailApp.sendEmail(row[2], "Code Admin Test", "This is a test to see if an email can be sent via app scripting");
}
});
}
function emailForLaptops() {
var ss=SpreadsheetApp.getActive();
var sheet1=ss.getSheetByName('Main');
var dt=new Date();
var tomorrow=Utilities.formatDate(new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()+1),Session.getScriptTimeZone(),"MM/dd/yyyy");
var active=sheet1.getActiveRange().getValues(); // gets the active highlighted data
var data=sheet1.getRange("C3:D21").getValues();
data.forEach(function(row) {
var r1dts=Utilities.formatDate(new Date(row[1]),Session.getScriptTimeZone(),"MM/dd/yyyy");
if (r1dts==tomorrow){
MailApp.sendEmail(row[2], "Code Admin Test", "This is a test to see if an email can be sent via app scripting");
}
});
}

File.makeCopy error

I am using the following code to make a copy of a file:
var backupFolder = DriveApp.getFolderById('1vOnO5aSOSFNWtOLHuAz4Lv2lsRgBwBpS');
var file = DriveApp.getFileById('1TJ_5Khw7wcqlIfbZ_-KLOQ-DsU02FsoRFYR1A3B4qAs');
file.makeCopy(file.getName(), backupFolder);
Most of the time that works without any issue.
Some times I get the following error:
We're sorry, a server error occurred. Please wait a bit and try again
Does anyone know how I can get a more meaningful error message to understand what is the issue?
To build on Anton's and Nancy's answers one useful approach I have found for error logging and catching important issues, is to append the error to a google sheet for reference with a timestamp and to send me a warning email with the failure reason. This gives me an instance notice something is not right, and helps me to keep track of issues.
function someImportantProcess(){
var ss = SpreadsheetApp.getActiveSpreadSheet();
var sheet = ss.getSpreadsheetByName('Sheet1');
try {
someImportant function(){
Logger.log('important stuff')
}
} catch(e){
sheet.appendRow([timestamp, e])
sendEmail(e)
}
}
sendEmail(e){
var subject = '[Important a script failed]';
var body = e;
sendEmail(myemail#google.com, subject, body);
}

Sending email as per desired format in Apps script

I have a script to send emails but it is not sending as per my desired format.
Can it be possible to send emails like this:-
Now my script is sending like this:-
TF,Unfunded,Platform,Link,,TF,Unfunded,,Data,Link,Date,Round,Funds,Domain,,Date,Round,Fundings,Domain,Changes Made,,,,,,7-May-02,Incorporation,2037.41,Shaji Vadakke Parambath,Vinay Gupta,Add round,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
My code is:
You can use a loop like this. If you want to get a better response, in the future I would recommend giving us access to a spreadsheet that has data consistent with the example and access to the script editor. It's difficult to copy code or data from an image. Which makes it less likely that someone will step up to help you. It would also be appreciated if you would take a stab at the code yourself. This code does work and I tested it with fake data.
This will comma separate all of the values in the row that it sends. The last column must be empty to qualify for sending and the 8 column must not be blank.
function SendingEmails()
{
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet();
var startRow=5;
var rg=sh.getDataRange();
var vA=rg.getValues();
var subject='Your Subject';
var emailaddress='Your Email Address';
for(var i=startRow-1;i<vA.length;i++)
{
if(!vA[i][vA[0].length-1] && vA[i][7])//not already sent and some data is available
{
//Logger.log('Send Mail: %s',formatAsTable(vA[i]));
//var userInterface=HtmlService.createHtmlOutput(formatAsTable(vA[i]));
//SpreadsheetApp.getUi().showModelessDialog(userInterface, 'Format As Table')
MailApp.sendEmail(emailaddres,subject,vA[i].join(','))
sh.getRange(i+1,vA[0].length).setValue('mailsent');
}
}
}
function formatAsTable(vA)
{
var s='<style>table{border:1px solid #000;}th,td{border:1px solid #000;}"</style><table><tr>';
for(var i=0;i<vA.length-1;i++)
{
s+='<td>' + vA[i] + '</td>';
}
s+='</tr></table>';
return s;
}
I suggest you install SheetConverter (created by David Bingham (Mogsdad)). Google it and it gives great instructions on how to install the library. The, I modified the example given to this:
function example() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var range = ss.getActiveSheet().getDataRange();
var conv = SheetConverter.init(ss.getSpreadsheetTimeZone(),
ss.getSpreadsheetLocale());
// Grab an array for formatted content
var array = conv.convertRange(range);
// Get a html table version, with all formatting
var html = conv.convertRange2html(range);
MailApp.sendEmail(Session.getActiveUser().getEmail(), 'Daily report','' ,{htmlBody: html})
}
It sent a test I set up of your data perfectly.