Align Hebrew text right to left in a script on Google Sheets - google-apps-script

I found on this forum a very useful script for sending emails from Google Sheets. But, unfortunately, when I send the emails, the message is in Hebrew and is written from left to right (instead of right to left).
In CSS, there is an "rtl" option. But in Javascript, I didn't found anything similar.
The script and the screenshot of the email are below. I appreciate your help.
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 2; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, 2);
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (var i in data) {
var row = data[i];
var emailAddress = row[0]; // First column
var message = row[1]; // Second column
var subject = 'קוד מוצר לרישיון שרכשת';
MailApp.sendEmail(emailAddress, subject, message);
}
}
Hebrew text aligned left to right instead of right to left

You could try setting the body as htmlBody instead using the text-align CSS property:
function example() {
var htmlbody = '<html><head><style>.text{text-align: right;}</style></head><body><div class="text">body of your email</div></body></html>';
MailApp.sendEmail({to:emailAddress, subject:subject, htmlBody:htmlbody});
}

Related

Sending Email using google script

Can anyone help me with sending an email via google scripts?
Here the challenge I am facing is that the range of the first column (email) may go up to 1000 lists of the email addresses. Although, now it's working fine (for now) how can I make it a dynamic range of list to be fed over my email lists to the script.
Code:
// This constant is written in column C for rows for which an email
// has been sent successfully.
var EMAIL_SENT = 'Email Success!';
/**
* Sends non-duplicate emails with data from the current spreadsheet.
*/
function sendEmails2() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Script (Beta)");
var startRow = 2; // First row of data to process
var numRows = 3; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, 3);
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[0]; // First column
var message = row[1]; // Second column
var emailSent = row[2]; // Third column
if (emailSent !== EMAIL_SENT) { // Prevents sending duplicates
var subject = '[Auto] The Process Has Not Yet Been Started';
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(startRow + i, 3).setValue(EMAIL_SENT);
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}
}
}
Doc Link
In order to make your range dynamic, you could use Sheet.getLastRow(), which returns the position of the last row with content.
Assuming that function sendEmails2 is the one your using for this, you would just have to modify the variable numRows. You would have to change this line:
var numRows = 3; // Number of rows to process
To this one:
var numRows = sheet.getLastRow() - startRow + 1;
Reference:
Sheet.getLastRow()
I hope this is of any help.

how to send a whole sheet using MailApp.sendEmail

Trying to send the contents of a sheet1, A:R using the MailApp.sendEmail. I can't seem to figure out how to get the data to break after each row
I've tried to define the range based on last row and column
function sendEmails() {
var sheet =
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("MissingInfo");
var Row = sheet.getLastRow()
var Col = sheet.getLastColumn();
var dataRange = sheet.getRange(1,1,Row,Col);
var data = dataRange.getValues();
var emailAddress = 'myemail#gmail.com';
var message = data
var subject = 'Sending emails from a Spreadsheet';
MailApp.sendEmail(emailAddress, subject, message);
}
This works, except the data, doesn't break at the end of each row. any ideas on how to have a line break?
data is a 2D array. You can join row arrays using newlines \n:
var message = data.join('\n');

Send email to the address in one column with body from another, when a keyword is present

I need to send mail notification to specific email address in Column A2 with body email from B2 when writing keyword in C2. Column A will be filled up from a Google Form.
https://docs.google.com/spreadsheets/d/15HrfUNhIyqLD948aRq0h9iAUW4Y_9LGe8HQm3MP2rZ0/edit?usp=sharing
function kirim() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 1; // First row of data to process
var numRows = 1; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, 2)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[0]; // First column
var message = row[1]; // Second column
var subject = "test meneh from spreadsheet";
MailApp.sendEmail(emailAddress, subject, message);
}
}
I dont't really know what the trigger column is for and there was any subject so I left it blank. But this should do what you need. You'll probably need to change the sheetname and if you want to call this with a timer based trigger you may want to replace the first line with var ss=SpreadsheetApp.openById('SpreadsheetID');
function kirim()
{
var ss=SpreadsheetApp.getActive();
var sht=ss.getSheetByName('SimpleEmail');
var rng=sht.getDataRange();
var rngA=rng.getValues();
var changed=false;
var re=/ok/i;
for(var i=1;i<rngA.length;i++)
{
if(rngA[i][2].match(re))//case insensitive and of the following will work: Ok ok oK OK
{
GmailApp.sendEmail(rngA[i][0], '', rngA[i][1]);
rngA[i][3]='EMAIL_SENT';
changed=true;
}
}
if(changed)rng.setValues(rngA);
}

Adding multiple lines of body in script editor

I am currently using the following code in a spreadsheet to send emails.
// This constant is written in column C for rows for which an email
// has been sent successfully.
var EMAIL_SENT = "EMAIL_SENT";
function sendEmails2() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 2; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, 3)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[0]; // First column
var message = row[1]; // Second column
var emailSent = row[2]; // Third column
if (emailSent != EMAIL_SENT) { // Prevents sending duplicates
var subject = "Sending emails from a Spreadsheet";
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(startRow + i, 3).setValue(EMAIL_SENT);
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}
}
}
In the above the message is only being processed from a single cell while i want it to use multiple cells.
Example:
the second column contains the body to be sent
how can i tweak the same.
Put the message in a single cell and format it as you wish within that cell. i.e. if you want to skip down a line or 2 hold control and press return and the cursor will go to the next line.
Get the cell in the script.
When you send the email the formatting will be retained.
You can also use '\n\n'on the script message to jump rows.

sendEmail function in Google Sheets

I have been struggling for days to get this appScript to function within my Google Sheets. Currently it is getting "stuck" on MailApp.sendEmail line, particularly on emailAddress. It gives me this error message "Invalid email: undefined". I based the majority of the script on their Sending Emails from a Spreadsheet Tutorial. So I'm not too sure where it might be going wrong.
Any help is much appreciated!
function sendEmails() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh0 = ss.getSheetByName("Candidates Ready for Offer");
var startRow = 2; // First row of data to process
var numRows = ss.getSheetByName("Candidates Ready for Offer").getLastRow()-1;
// Fetch recent additions
var dataRange = ss.getSheetByName("Candidates Ready for Offer").getRange(startRow, 3)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[5];
var message = "row[1]" + "row [2]" + "at" + "row[4]" + "is ready for an offer!"
var subject = "Candidate is Ready for Offer";
MailApp.sendEmail(emailAddress, subject, message);
}
}
Thanks for sharing your sheet. There was actually a number of reasons why it wasn't working which I'll try and explain. Anyway, your script on the shared sheet will now function as you want it to. Here is what it looks like:
function sendEmails() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh0 = ss.getSheetByName("Candidates Ready for Offer");
var startRow = 4; // First row of data to process
var numRows = sh0.getLastRow()-startRow+1;
// Fetch recent additions
var dataRange = sh0.getRange(startRow,1,numRows,6);
// Fetch values for each row in the Range.
var data = dataRange.getValues();
Logger.log(data);
for (i in data) {
var row = data[i];
var emailAddress = row[3];
Logger.log(emailAddress);
var message = "row[1]" + "row [2]" + "at" + "row[4]" + "is ready for an offer!"
var subject = "Candidate is Ready for Offer";
MailApp.sendEmail(emailAddress, subject, message);
}
}
There's no need to keep calling the same sheet when you've already defined it as the variable sh0
The data range was including the first 3 rows. You didn't want this. Some simple maths in the var numRows allows the spreadsheet to only take the range containing data starting at row 4.
You were referencing the wrong column for email address. It was in column 4.
I hope this helps/makes sense.
Oli
The problem is your var dataRange. At the moment your code is only getting the range of one cell (startrow,3) instead of an array.
Replacing .getRange(startrow,3) with getDataRange() should fix your issue.