Limit Script to one sheet - google-apps-script

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

Related

Add data to the bottom of a range

So I'm currently trying to set up a simple button which moves data from a range into another sheet for future reference. I've got it working if the sheet it is going to contains no information but I wanted to put all of the references into one sheet together.
Is there a way to add to the bottom of a range of data? This is what I have at the moment.
function Addvoucher() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var target_sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Meta');
var source_sheet = ss.getSheetByName("Tools");
var source_range = source_sheet.getRange("B9:D9");
var target_range = target_sheet.getRange("S3:U3");
var last_row = target_sheet.getLastRow();
var target_range = target_sheet.getRange("S3" + (last_row + 1) + ":U3" + (last_row + 1));
target_sheet.insertRowAfter(last_row);
source_range.copyTo(target_range);
}
Modification points:
I think that in your script, how about modifying "S3" + (last_row + 1) + ":U3" + (last_row + 1) of target_sheet.getRange("S3" + (last_row + 1) + ":U3" + (last_row + 1)) as follows?
In the current script, for example, last_row + 1 is 2, "S3" + (last_row + 1) + ":U3" + (last_row + 1) becomes S32:U32. In this case, it is required to be S2:U2. I think that this might be the reason of your issue.
Tried this, the issue is there is data in other columns to the left of this "block" of information thus this gets put onto row 100 as its the first available row.
From your replying, I could understand that in your Spreadsheet, the columns except for the columns "U" to "S" has the values and the last row of the columns "U" to "S" is different from other columns.
When above points as reflected to your script, I would like to propose to modify as follows.
From:
var last_row = target_sheet.getLastRow();
var target_range = target_sheet.getRange("S3" + (last_row + 1) + ":U3" + (last_row + 1));
To:
var last_row = target_sheet.getLastRow();
var values = target_sheet.getRange("S1:U" + last_row).getValues();
for (var i = values.length - 1; i >= 0; i--) {
if (values[i].every(e => e.toString() != "")) {
last_row = i + 1;
break;
}
}
var target_range = target_sheet.getRange("S" + (last_row + 1) + ":U" + (last_row + 1));

How do I loop through all rows and send one email with all row data?

My current script is able to send an email, but it sends multiple email with one row of data in each email. I want the script to be able to send one email with all rows of data.
function tazEmail() {
var sheet = SpreadsheetApp.openById("1naSWINA8_uxeLUsj0lFntqILyDj2nirb56uvkBel79Y").getSheetByName("Sheet3");
//var ss = SpreadsheetApp.getActive().getSheetByName("CRM Feedback");
var data = sheet.getRange(2, 1, sheet.getLastRow(), sheet.getLastColumn()).getValues();
var manager_email = "email#email.org.au";
for( var i = 1; i< data.length;i++ ) {
var timestamp = data[i][0];
var requesterEmail = data[i][1];
var starRating = data[i][2];
var requestCatergory = data[i][3];
var description = data[i][4];
var label = data[i][5];
var ticketId = data[i][6];
var comment = data[i][7];
var status = data[i][8];
var priority = data[i][9];
var office = data[i][10];
var message = "";
var check = data[i][11];
if (check == "complete"){
continue;
}else {
var subject = "CT IT feedback - ";
var body = "<body>";
message +=
"<p><b>Timestamp by: </b>" + data[i][0] + "</p>" +
"<p><b>Requester Email: </b>" + data[i][1] + "</p>" +
"<p><b>Star Rating: </b>" + data[i][2] + "</p>" +
"<p><b>Request Category: </b>" + data[i][3] + "</p>" +
"<p><b>Description: </b>" + data[i][4] + "</p>" +
"<p><b>Label: </b>" + data[i][5] + "</p>" +
"<p><b>Ticket ID: </b>" + data[i][6] + "</p>" +
"<p><b>Comment: </b>" + data[i][7] + "</p>" +
"<p><b>Status: </b>" + data[i][8] + "</p><br><br>";
MailApp.sendEmail({
to: manager_email,
subject: subject,
htmlBody: message,
});
}
}
}
Try:
function tazEmail() {
var sheet = SpreadsheetApp.openById("1naSWINA8_uxeLUsj0lFntqILyDj2nirb56uvkBel79Y").getSheetByName("Sheet3");
//var ss = SpreadsheetApp.getActive().getSheetByName("CRM Feedback");
var data = sheet.getRange(2, 1, sheet.getLastRow(), sheet.getLastColumn()).getValues();
var manager_email = "email#email.org.au";
var message = "";
var subject = "CT IT feedback - ";
var body = "<body>"; // Don't think this is being used. May be deleted.
for( var i = 1; i< data.length;i++ ) {
var timestamp = data[i][0];
var requesterEmail = data[i][1];
var starRating = data[i][2];
var requestCatergory = data[i][3];
var description = data[i][4];
var label = data[i][5];
var ticketId = data[i][6];
var comment = data[i][7];
var status = data[i][8];
var priority = data[i][9];
var office = data[i][10];
var check = data[i][11];
if (check == "complete"){
continue;
}else {
message +=
"<p><b>Timestamp by: </b>" + data[i][0] + "</p>" +
"<p><b>Requester Email: </b>" + data[i][1] + "</p>" +
"<p><b>Star Rating: </b>" + data[i][2] + "</p>" +
"<p><b>Request Category: </b>" + data[i][3] + "</p>" +
"<p><b>Description: </b>" + data[i][4] + "</p>" +
"<p><b>Label: </b>" + data[i][5] + "</p>" +
"<p><b>Ticket ID: </b>" + data[i][6] + "</p>" +
"<p><b>Comment: </b>" + data[i][7] + "</p>" +
"<p><b>Status: </b>" + data[i][8] + "</p><br><br>";
}
}
MailApp.sendEmail({
to: manager_email,
subject: subject,
htmlBody: message
});
}

Script running incorrectly for email sent when specific column has data

I have the below script that is running but marks the "Email_Sent" column for every row (all 1000), I would like it to only send an email if Column A, B or C has an entry. (This is copied from the first sheet if someone marks a Yes in a specific column) and only if the Sent_Email column is blank as well.
function sendEmails()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.setActiveSheet(ss.getSheetByName("Email"));
var sheet = SpreadsheetApp.getActiveSheet();
var dataRange = sheet.getDataRange();
var data = dataRange.getValues();
for (var i = 1; i < data.length; ++i)
{
var rowData = data[i];
var emailAddress = rowData[1];
var recipient = rowData[0];
var message1 = rowData[3];
var message2 = rowData[4];
var message3 = rowData[5];
var message4 = rowData[6];
var message5 = rowData[7];
var emailSent = rowData[9];
var message = 'Hi ' + recipient + ',\n\n' + message1 + ' ' + message2 +
',\n\n' + message3 + ',\n' + message4 + ',\n' + message5;
var subject = 'Medical Questionairre Check';
if (emailSent != 'EMAIL_SENT' && MailApp.getRemainingDailyQuota()>0 &&
emailAddress && subject && message)
{
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(i+1, 9).setValue('EMAIL_SENT');// Make sure the cell is
updated right away in case the script is interrupted
}
}
}
Here is the link to the spreadsheet.
https://docs.google.com/spreadsheets/d/1SOPiXhU3KWHpHyZEiDUSk8m5qzO5CyALWio6doQvJ4Q/edit?usp=sharing
A Simple Fix
Just had to change emailSent = rowData[25] rather than rowData[9]
function sendEmails()
{
var ss=SpreadsheetApp.getActive();
var sheet=ss.getSheetByName('Sheet2');
var dataRange=sheet.getDataRange();
var data=dataRange.getValues();
for (var i=1;i<data.length;++i)
{
var rowData = data[i];
var emailAddress = rowData[1];
var recipient = rowData[0];
var message1 = rowData[3];
var message2 = rowData[4];
var message3 = rowData[5];
var message4 = rowData[6];
var message5 = rowData[7];
var emailSent = rowData[25];//Change from 9 to 25
var message = 'Hi ' + recipient + ',\n\n' + message1 + ' ' + message2 + ',\n\n' + message3 + ',\n' + message4 + ',\n' + message5;
var subject = 'Medical Questionairre Check';
var rdq=MailApp.getRemainingDailyQuota();
if (!emailSent && rdq>0 && emailAddress && subject && message)
{
MailApp.sendEmail(emailAddress, subject, message)
//Logger.log('emailAddress: %s subject: %s message: %s emailSent: %s',emailAddress,subject,message,emailSent);
sheet.getRange(i+1, 26).setValue('EMAIL_SENT');
}
}
}
The spreadsheet after execution:

Need to Set_Formula on Insert_Row from IFTTT

Novice Google Apps Scripter here,
I have an IFTTT applet which adds a row to this spreadsheet via email: Data Test
I seem to have the formulas set up correctly, but when a new row is added, the formulas obviously do not auto-populate into that new row. When a row is inserted, in which the corresponding cells in Columns A and B are not blank, I'd like set certain formulas in that row.
The script I have so far (see below) does give me the formulas I want, but only in Row1.
I'd like the script to set those same formulas into corresponding cells of any new row that is inserted.
For example, IFTTT.com automation will populate cells A6 and B6 with text (i.e., next blank row in linked spreadsheet) -- I need all of the formulas currently entered to then apply to B6 (as opposed to B2)
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var cell = sheet.getRange("C1");
cell.setFormula('=IFERROR(MID($B2,SEARCH("details",$B2)+7,SEARCH(",",$B2)-SEARCH("details",$B2)-7),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
var cell = sheet.getRange("D1");
cell.setFormula('=IFERROR(TRIM(LEFT(SUBSTITUTE(MID(B2,FIND("$",B2),LEN(B2))," ",REPT(" ",100)),100)),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
var cell = sheet.getRange("E1");
cell.setFormula('=IFERROR(MID($B2,SEARCH("exceed",$B2)+7,SEARCH("%",$B2)-SEARCH("exceed",$B2)-6),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
var cell = sheet.getRange("F1");
cell.setFormula('=IFERROR(MID($B2,SEARCH("due",$B2)+3,SEARCH(";",$B2)-SEARCH("due",$B2)-3),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
var cell = sheet.getRange("G1");
cell.setFormula('=IFERROR(MID($B2,SEARCH("held on",$B2)+7,SEARCH(". Lottery",$B2)-SEARCH("held on",$B2)-7),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
var cell = sheet.getRange("H1");
cell.setFormula('=IFERROR(MID($B2,SEARCH("posted by",$B2)+9,SEARCH(". ",$B2)-SEARCH("",$B2)-167),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
}
Any assistance will be greatly appreciated!
Try this out:
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.
}
}
This is something that I've done in the past. I was surprised to learn that there was a setFormula command because I didn't use it in this situation and the below technique is working fine.
for(var i = 0;i < rowA[0].length; i++)
{
var initial_value = rowA[0][i];
//rowA[0][i] = '=ArrayFormula(IF(Row($B:$B)=1,"' + initial_value + '",IF(LEN($B:$B),IF(REGEXMATCH($C:$C,"(?i)(' + initial_value + ')"),$D:$D,""),)))';
rowA[0][i] = '=ArrayFormula(IF(Row(' + sr + ')=1,"' + initial_value + '",IF(LEN(' + sr + '),IF(REGEXMATCH(' + sr + ',"(?i)(' + initial_value + ')"),' + vr + ',""),)))';
}
rowrng.setValues(rowA);
It's a little different from what your doing in that I'm setting all of the array values before running the setValues command. But this has been working for months.

Can I use Insert_Row as trigger for cell.setFormula?

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"))');
}
}