I have a google sheet containing 4 columns; title, url, published date and email sent (in that exact order).
When new rows are added to the sheet i want to execute a google script that will look through the 'email sent' column to see if an email has been sent and if not send an email containing the new rows and update the associated row(s) with a yes.
My current code is only getting the first row and nothing else.
Thanks in advance,
Mark
(see my current code below)
function sendemail() {
//setup function
var ActiveSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var StartRow = 2;
var RowRange = ActiveSheet.getLastRow() - StartRow + 1;
var WholeRange = ActiveSheet.getRange(StartRow,1,RowRange,4);
var AllValues = WholeRange.getValues();
//iterate loop
for (i in AllValues) {
//set current row
var CurrentRow = AllValues[i];
//set subject line
var Subject = "New Content on IG.com";
//set HTML template for information
var message =
"<p><b>Title: </b>" + CurrentRow[1] + "</p>" +
"<p><b>Article: </b>" + CurrentRow[2] + "</p>" +
"<p><b>Published Date: </b>" + CurrentRow[3] + "</p>";
//define column to check if sent
var EmailSent = CurrentRow[4];
//define who to send grants to
var SendTo = "TEST#gmail.com";
//if row has not been sent, then...
if (EmailSent != "Yes") {
//set the row to look at
var setRow = parseInt(i) + StartRow;
//mark row as "sent"
ActiveSheet.getRange(setRow, 4).setValue("Yes");
//send the actual email
MailApp.sendEmail({
to: SendTo,
cc: "",
subject: Subject,
htmlBody: message,
});
}
}
}
Try this:
function sendemail() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getActiveSheet();
const sr = 2;
const rg = sh.getRange(sr, 1, sh.getLastRow() - sr + 1, 4);
const vs = rg.getValues();
vs.forEach((r, i) => {
let Subject = "New Content on IG.com";
let message =
"<p><b>Title: </b>" + r[0] + "</p>" +
"<p><b>Article: </b>" + r[1] + "</p>" +
"<p><b>Published Date: </b>" + r[2] + "</p>";
let EmailSent = r[3];
let SendTo = "TEST#gmail.com";
if (EmailSent != "Yes") {
sh.getRange(i + sr, 4).setValue("Yes");
MailApp.sendEmail({to: SendTo,cc: "",subject: Subject,htmlBody: message});
}
})
}
Test:
A
B
C
D
1
Title
url
date
Sent
2
t1
u1
d1
Yes
3
t1
u2
d2
Yes
4
t1
u3
d3
Yes
5
t1
u4
d4
Yes
6
t1
u5
d5
Yes
7
t1
u6
d6
Yes
8
t1
u7
d7
Yes
9
t1
u8
d8
Yes
10
t1
u9
d9
Yes
It turns then all to Yes
This script does exactly what I'm looking for with my data, however I'd like to limit it to run only on one sheet. I've tried using if (s.getName() == 'Sheet1') immediately after var sheet = ss.getSheets()[0] but then the script no longer functions properly.
What am I missing?
function onEdit(e){
var ui = SpreadsheetApp.getUi();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = e.range;
var columnOfCellEdited = range.getColumn();
if (columnOfCellEdited === 1)
var range2 = range.getRow();
var destrange = range2;
// adds the formulas
var cell = sheet.getRange("C" + destrange);
cell.setFormula('=IFERROR(MID($B' + destrange +',SEARCH("details",$B' + destrange +')+7,SEARCH(",",$B' + destrange +')-SEARCH("details",$B' + destrange +')-7),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
var cell = sheet.getRange("D" + destrange);
cell.setFormula('=IFERROR(TRIM(LEFT(SUBSTITUTE(MID(B' + destrange +',FIND("$",B' + destrange +'),LEN(B' + destrange +'))," ",REPT(" ",100)),100)),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
var cell = sheet.getRange("E" + destrange);
cell.setFormula('=IFERROR(MID($B' + destrange +',SEARCH("exceed",$B' + destrange +')+7,SEARCH("%",$B' + destrange +')-SEARCH("exceed",$B' + destrange +')-6),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
var cell = sheet.getRange("F" + destrange);
cell.setFormula('=IFERROR(MID($B' + destrange +',SEARCH("due",$B' + destrange +')+3,SEARCH(";",$B' + destrange +')-SEARCH("due",$B' + destrange +')-3),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
var cell = sheet.getRange("G" + destrange);
cell.setFormula('=IFERROR(MID($B' + destrange +',SEARCH("held on",$B' + destrange +')+7,SEARCH(". Lottery",$B' + destrange +')-SEARCH("held on",$B' + destrange +')-7),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
var cell = sheet.getRange("H" + destrange);
cell.setFormula('=IFERROR(MID($B' + destrange +',SEARCH("posted by",$B' + destrange +')+9,SEARCH(". ",$B' + destrange +')-SEARCH("",$B' + destrange +')-167),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
}
You can do this to make sure the function acts only on "sheet1"
var range = e.range;
var sheet = range.getSheet()
var sheetName = sheet.getName()
if(sheetName != "Sheet1"){
return //exit function
}
Use event object to get the range>Sheet>SheetName. Compare the sheet name to sheet1 if different exit function
Full code:
function onEdit(e){
var ui = SpreadsheetApp.getUi();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var range = e.range;
var sheet = range.getSheet()
var sheetName = sheet.getName()
if(sheetName != "Sheet1"){
return //exit function
}
var columnOfCellEdited = range.getColumn();
if (columnOfCellEdited === 1)
var range2 = range.getRow();
var destrange = range2;
// adds the formulas
var cell = sheet.getRange("C" + destrange);
cell.setFormula('=IFERROR(MID($B' + destrange +',SEARCH("details",$B' + destrange +')+7,SEARCH(",",$B' + destrange +')-SEARCH("details",$B' + destrange +')-7),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
var cell = sheet.getRange("D" + destrange);
cell.setFormula('=IFERROR(TRIM(LEFT(SUBSTITUTE(MID(B' + destrange +',FIND("$",B' + destrange +'),LEN(B' + destrange +'))," ",REPT(" ",100)),100)),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
var cell = sheet.getRange("E" + destrange);
cell.setFormula('=IFERROR(MID($B' + destrange +',SEARCH("exceed",$B' + destrange +')+7,SEARCH("%",$B' + destrange +')-SEARCH("exceed",$B' + destrange +')-6),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
var cell = sheet.getRange("F" + destrange);
cell.setFormula('=IFERROR(MID($B' + destrange +',SEARCH("due",$B' + destrange +')+3,SEARCH(";",$B' + destrange +')-SEARCH("due",$B' + destrange +')-3),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
var cell = sheet.getRange("G" + destrange);
cell.setFormula('=IFERROR(MID($B' + destrange +',SEARCH("held on",$B' + destrange +')+7,SEARCH(". Lottery",$B' + destrange +')-SEARCH("held on",$B' + destrange +')-7),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
var cell = sheet.getRange("H" + destrange);
cell.setFormula('=IFERROR(MID($B' + destrange +',SEARCH("posted by",$B' + destrange +')+9,SEARCH(". ",$B' + destrange +')-SEARCH("",$B' + destrange +')-167),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
}
}
Hope that helps
Novice Google Apps Scripter here,
I have an IFTTT applet which adds a row to this spreadsheet via email: Data Test
Thanks to the amazing StackOverflow community, I'm now able to manually add a row which sets up the needed formulas with the correct reference, I'd like the script to automatically set those same formulas into corresponding cells of any new row that is inserted.
For example, my IFTTT.com automation will populate cells A6 and B6 with text (i.e., creating next blank row in linked spreadsheet) -- I need all of the formulas currently entered to then apply to B6 (as opposed to B2)
var ss = SpreadsheetApp.getActiveSpreadsheet();
function onOpen() {
var items = [
{name: 'Add Row', functionName: 'addrow'},
];
ss.addMenu('Add Row', items);
}
function addrow() {
var ui = SpreadsheetApp.getUi();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var result = ui.prompt(
'Enter number of Row',
ui.ButtonSet.OK_CANCEL);
// Process the user's response.
var button = result.getSelectedButton();
var rownum = result.getResponseText();
if (button == ui.Button.OK) {
// User clicked "OK".
sheet.insertRowAfter(rownum);
var cell = sheet.getRange("C" + rownum);
cell.setFormula('=IFERROR(MID($B' + rownum +',SEARCH("details",$B' + rownum +')+7,SEARCH(",",$B' + rownum +')-SEARCH("details",$B' + rownum +')-7),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
var cell = sheet.getRange("D" + rownum);
cell.setFormula('=IFERROR(TRIM(LEFT(SUBSTITUTE(MID(B' + rownum +',FIND("$",B' + rownum +'),LEN(B' + rownum +'))," ",REPT(" ",100)),100)),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
var cell = sheet.getRange("E" + rownum);
cell.setFormula('=IFERROR(MID($B' + rownum +',SEARCH("exceed",$B' + rownum +')+7,SEARCH("%",$B' + rownum +')-SEARCH("exceed",$B' + rownum +')-6),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
var cell = sheet.getRange("F" + rownum);
cell.setFormula('=IFERROR(MID($B' + rownum +',SEARCH("due",$B' + rownum +')+3,SEARCH(";",$B' + rownum +')-SEARCH("due",$B' + rownum +')-3),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
var cell = sheet.getRange("G" + rownum);
cell.setFormula('=IFERROR(MID($B' + rownum +',SEARCH("held on",$B' + rownum +')+7,SEARCH(". Lottery",$B' + rownum +')-SEARCH("held on",$B' + rownum +')-7),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
var cell = sheet.getRange("H" + rownum);
cell.setFormula('=IFERROR(MID($B' + rownum +',SEARCH("posted by",$B' + rownum +')+9,SEARCH(". ",$B' + rownum +')-SEARCH("",$B' + rownum +')-167),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
} else if (button == ui.Button.CANCEL) {
// User clicked "Cancel".
} else if (button == ui.Button.CLOSE) {
// User clicked X in the title bar.
}
}
To be clear, what I'd like to do (instead of adding a menu to manually insert a row) is have a script that detects whenever a new row is inserted, triggering the following:
var cell = sheet.getRange("C" + rownum);
cell.setFormula('=IFERROR(MID($B' + rownum +',SEARCH("details",$B' + rownum +')+7,SEARCH(",",$B' + rownum +')-SEARCH("details",$B' + rownum +')-7),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
var cell = sheet.getRange("D" + rownum);
cell.setFormula('=IFERROR(TRIM(LEFT(SUBSTITUTE(MID(B' + rownum +',FIND("$",B' + rownum +'),LEN(B' + rownum +'))," ",REPT(" ",100)),100)),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
var cell = sheet.getRange("E" + rownum);
cell.setFormula('=IFERROR(MID($B' + rownum +',SEARCH("exceed",$B' + rownum +')+7,SEARCH("%",$B' + rownum +')-SEARCH("exceed",$B' + rownum +')-6),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
var cell = sheet.getRange("F" + rownum);
cell.setFormula('=IFERROR(MID($B' + rownum +',SEARCH("due",$B' + rownum +')+3,SEARCH(";",$B' + rownum +')-SEARCH("due",$B' + rownum +')-3),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
var cell = sheet.getRange("G" + rownum);
cell.setFormula('=IFERROR(MID($B' + rownum +',SEARCH("held on",$B' + rownum +')+7,SEARCH(". Lottery",$B' + rownum +')-SEARCH("held on",$B' + rownum +')-7),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
var cell = sheet.getRange("H" + rownum);
cell.setFormula('=IFERROR(MID($B' + rownum +',SEARCH("posted by",$B' + rownum +')+9,SEARCH(". ",$B' + rownum +')-SEARCH("",$B' + rownum +')-167),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
Any assistance will be greatly appreciated!
This works anytime the spreadsheet is edited. You may have to test it as "ANYTIME" it is edited it adds the functions. Does not seem the best way to do it, however it is somewhat what you were looking for.
function onEdit(e){
var ui = SpreadsheetApp.getUi();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = e.range;
var range2 = range.getRow();
var destrange = range2 + 1;
// inserts blank row after last edit.
sheet.insertRowAfter(range2);
// adds the formulas
var cell = sheet.getRange("C" + destrange);
cell.setFormula('=IFERROR(MID($B' + destrange +',SEARCH("details",$B' + destrange +')+7,SEARCH(",",$B' + destrange +')-SEARCH("details",$B' + destrange +')-7),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
var cell = sheet.getRange("D" + destrange);
cell.setFormula('=IFERROR(TRIM(LEFT(SUBSTITUTE(MID(B' + destrange +',FIND("$",B' + destrange +'),LEN(B' + destrange +'))," ",REPT(" ",100)),100)),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
var cell = sheet.getRange("E" + destrange);
cell.setFormula('=IFERROR(MID($B' + destrange +',SEARCH("exceed",$B' + destrange +')+7,SEARCH("%",$B' + destrange +')-SEARCH("exceed",$B' + destrange +')-6),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
var cell = sheet.getRange("F" + destrange);
cell.setFormula('=IFERROR(MID($B' + destrange +',SEARCH("due",$B' + destrange +')+3,SEARCH(";",$B' + destrange +')-SEARCH("due",$B' + destrange +')-3),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
var cell = sheet.getRange("G" + destrange);
cell.setFormula('=IFERROR(MID($B' + destrange +',SEARCH("held on",$B' + destrange +')+7,SEARCH(". Lottery",$B' + destrange +')-SEARCH("held on",$B' + destrange +')-7),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
var cell = sheet.getRange("H" + destrange);
cell.setFormula('=IFERROR(MID($B' + destrange +',SEARCH("posted by",$B' + destrange +')+9,SEARCH(". ",$B' + destrange +')-SEARCH("",$B' + destrange +')-167),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
}
As suggested in this related SO post - Google Script: function that insert new row copying functions/formulas from last row, try using getFormula() from previous row and setFormula() to the new row.
getFomula() code :
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// This assumes we have a function in B5 that sums up
// B2:B4
var range = sheet.getRange("B5");
// Logs the calculated value and the formula
Logger.log("Calculated value: %s Formula: %s",
range.getValue(),
range.getFormula());
setFormula() code:
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var cell = sheet.getRange("B5");
cell.setFormula("=SUM(B3:B4)");
Hope this helps.
A different way to do this- Now if the last row column B is not blank it will fill in formulas. Works on each edit.
function onEdit(){
var ui = SpreadsheetApp.getUi();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var lastRow = ss.getLastRow();
var lastRow2 = lastRow+1;
var s = ss.getSheetByName('sheet1');
var lRow = s.getRange(lastRow, 2).getValue();
var newRow = s.getRange(lastRow2, 2).getValue();
if(lRow != ""){
// adds the formulas
var cell = sheet.getRange("C" + lastRow2);
cell.setFormula('=IFERROR(MID($B' + lastRow2 +',SEARCH("details",$B' + lastRow2 +')+7,SEARCH(",",$B' + lastRow2 +')-SEARCH("details",$B' + lastRow2 +')-7),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
var cell = sheet.getRange("D" + lastRow2);
cell.setFormula('=IFERROR(TRIM(LEFT(SUBSTITUTE(MID(B' + lastRow2 +',FIND("$",B' + lastRow2 +'),LEN(B' + lastRow2 +'))," ",REPT(" ",100)),100)),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
var cell = sheet.getRange("E" + lastRow2);
cell.setFormula('=IFERROR(MID($B' + lastRow2 +',SEARCH("exceed",$B' + lastRow2 +')+7,SEARCH("%",$B' + lastRow2 +')-SEARCH("exceed",$B' + lastRow2 +')-6),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
var cell = sheet.getRange("F" + lastRow2);
cell.setFormula('=IFERROR(MID($B' + lastRow2 +',SEARCH("due",$B' + lastRow2 +')+3,SEARCH(";",$B' + lastRow2 +')-SEARCH("due",$B' + lastRow2 +')-3),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
var cell = sheet.getRange("G" + lastRow2);
cell.setFormula('=IFERROR(MID($B' + lastRow2 +',SEARCH("held on",$B' + lastRow2 +')+7,SEARCH(". Lottery",$B' + lastRow2 +')-SEARCH("held on",$B' + lastRow2 +')-7),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
var cell = sheet.getRange("H" + lastRow2);
cell.setFormula('=IFERROR(MID($B' + lastRow2 +',SEARCH("posted by",$B' + lastRow2 +')+9,SEARCH(". ",$B' + lastRow2 +')-SEARCH("",$B' + lastRow2 +')-167),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
}
}
I'm trying to create a function to pull data from google finance and have it automatically log data to multiple sheets of a document. I can't seem to get it to add a function as a function it always adds it as text until i edit the cell
function addLog() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var numLastRows = rows.getLastRow();
sheet.appendRow(['2012/10/28','=b92','=c92','Log (auto)','','=index(GoogleFinance(C92,"price",A92);2;2)','','=h92','=i92','=j92','=k92']);
};
i couldn't figure out how to make it reference the cell above (row 92 in this case) either :(
You will need to use the setFormula() method rather than appendRow(). Perhaps something like:
function addLog() {
var sheet = SpreadsheetApp.getActiveSheet();
var numLastRow = sheet.getLastRow();
//add a row at end if necessary
if (sheet.getMaxRows() == numLastRow) sheet.insertRowAfter(numLastRow);
sheet.getRange(numLastRow + 1, 2, 1, 10)
.setFormulas([['=b' + numLastRow,
'=c' + numLastRow,
'',
'',
'=index(GoogleFinance(C' + numLastRow + ',"price",A' + numLastRow + ');2;2)',
'',
'=h' + numLastRow,
'=i' + numLastRow,
'=j' + numLastRow,
'=k' + numLastRow]]);
sheet.getRange(numLastRow + 1, 1).setValue('2012/10/28');
sheet.getRange(numLastRow + 1, 4).setValue('Log (auto)');
}
To perform this action from the xth sheet through to the yth sheet (using zero-based index), I think the most efficient way would be:
function addAllLog() {
var sheets = SpreadsheetApp.getActive().getSheets();
var x = 1;
var y = 34;
for (var i = x; i <= y; i++) {
addLog(sheets[i]);
}
}
function addLog(sheet) {
var numLastRow = sheet.getLastRow();
//add a row at end if necessary
if (sheet.getMaxRows() == numLastRow) sheet.insertRowAfter(numLastRow);
sheet.getRange(numLastRow + 1, 2, 1, 10)
.setFormulas([['=b' + numLastRow,
'=c' + numLastRow,
'',
'',
'=index(GoogleFinance(C' + numLastRow + ',"price",A' + numLastRow + ');2;2)',
'',
'=h' + numLastRow,
'=i' + numLastRow,
'=j' + numLastRow,
'=k' + numLastRow]]);
sheet.getRange(numLastRow + 1, 1).setValue('2012/10/28');
sheet.getRange(numLastRow + 1, 4).setValue('Log (auto)');
}