How can i get all objects in a column Google Spreadsheet - javascript

I have the column "D" in my spreadsheet. I would like to get all objects in the column and send an email to them. My script gets the firts user in the column and then loops it, it dosen't continue.
This i what i have:
function email(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var dataSheet = ss.getSheets()[0];
var email = dataSheet.getRange(2, 5);
var email2 = email.getValue();
for (var i = 0; i < email2.length; ++i) {
// Get a row object
var rowData = email2[i];
MailApp.sendEmail(rowData emailSubject, emailText);
Logger.log(email2);
}
}

you selected only one cell, so you are making a loop in a single element...
try this way :
function email(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var dataSheet = ss.getSheets()[0];
var emails = dataSheet.getRange('D2:D').getValues();
for (var i = 0; i < emails.length; ++i) {
// Get a row object
var rowData = emails[i][0];
if(rowData!=''){
MailApp.sendEmail(rowData emailSubject, emailText);// I assume emailSubject and emailText are defined in your real code...
}
Logger.log(emails[i]);
}
}

Related

Update or Replace Row data in other sheet when unique id is found - Google Appscript

I need someone to modify this script that I found here to update or replace the row value of the sheet2 from sheet1 when a unique ID is found. This only works up to the 2nd column..I need something to update or replace rows in the sheet2 up to 10 columns or more. I have a sample spreadsheet. If you see on sheet2, some row values are missing or not yet updated, so I need those rows to be updated or replaced what's in sheet1.
Here is the spreadsheet:
https://docs.google.com/spreadsheets/d/100sjGCr0HSdJE1AERCJ9bGKiu3Vs2KxrvGIIjIrOjHw/edit?usp=sharing
Thanks for helping and sharing your knowledge.
function updateEntrees() {
var ss=SpreadsheetApp.getActive();
var sh1=ss.getSheetByName('Sheet1');
var rg1a=sh1.getRange(2,1,sh1.getLastRow()-1,1);
var vA1a=rg1a.getValues();
var rg1b=sh1.getRange(2,2,sh1.getLastRow()-1,1);
var vA1b=rg1b.getValues();
var sh2=ss.getSheetByName('Sheet2');
var rg2a=sh2.getRange(2,1,sh2.getLastRow()-1,1);
var vA2a=rg2a.getValues();
var rg2b=sh2.getRange(2,2,sh2.getLastRow()-1,1);
var vA2b=rg2b.getValues();
for(var i=0;i<vA1a.length;i++) {
for(var j=0;j<vA2a.length;j++) {
if(vA1a[i][0]==vA2a[j][0]) {
vA2b[j][0]=vA1b[i][0]
}
}
}
rg2b.setValues(vA2b);
Try the following code, in the second line, change the value of "numColumns" as per your need:
function updateEntrees() {
var numColumns = 9;
var ss = SpreadsheetApp.getActive();
var sh1 = ss.getSheetByName('Sheet1');
var rg1a = sh1.getRange(2,1,sh1.getLastRow()-1,1);
var vA1a = rg1a.getValues();
var rg1b = sh1.getRange(2,2,sh1.getLastRow()-1,numColumns);
var vA1b = rg1b.getValues();
var sh2 = ss.getSheetByName('Sheet2');
var rg2a = sh2.getRange(2,1,sh2.getLastRow()-1,1);
var vA2a = rg2a.getValues();
var rg2b = sh2.getRange(2,2,sh2.getLastRow()-1,numColumns);
var vA2b = rg2b.getValues();
for( var i=0; i < vA1a.length; i++ ) {
for( var j=0; j < vA2a.length; j++ ) {
if( vA1a[i][0] == vA2a[j][0] ) {
vA2b[j] = vA1b[i];
}
}
}
rg2b.setValues(vA2b);
};

Google Script, find match value from one column with another column

I am try to make "Color check",
It will change color when value input is detected.
I need to find match value from one column with another column.
Find match for each value in each value in the column.
But my code dont work, can anyone help with my code?
Here is my code:
function checkScriptCheck() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var datePaymentValues = sheet.getRange("A:A").getValues();
var dateInputValues = sheet.getRange("B:B").getValues();
var datePaymentRange = sheet.getRange("A:A");
var datePaymentColumn = sheet.getRange("A:A").getColumn();
var checkResultValueColumn =sheet.getRange("C:C").getColumn()
//cleaning color
datePaymentRange.setBackground("white");
//check and coloring
for(i=0;i<datePaymentValues.length;i++){
for(j=0;j<dateInputValues.length;j++){
if(datePaymentValues[i][0]==dateInputValues[j][0]){
sheet.getRange(i+1, datePaymentColumn).setBackground("green");
sheet.getRange(i+1, checkResultValueColumn).setValue("check");
};
};
};
};
Here is the link to my sheet:
https://docs.google.com/spreadsheets/d/1DVbNaehsTWkiIkzW2nQx7w-ZB8CrPmSP5T5CpU24mbU/edit?usp=sharing
Here is some screenshoot:
Sheet Screenshoot
Code ScreenShoot
Thankyou.
Will be easier if you create a map of values to check for first.
function checkScriptCheck() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");
var datePaymentRange = sheet.getRange("A:A");
var datePaymentValues = datePaymentRange.getValues();
var dateInputValues = sheet.getRange("B:B").getValues();
var datePaymentColumn = datePaymentRange.getColumn();
var checkResultValueColumn =sheet.getRange("C:C").getColumn()
//cleaning color
datePaymentRange.setBackground("white");
//create map of values to check for
var inputDates = {};
for (var i = 1; i < dateInputValues.length; i++) { // Exclude header row
var inputDate = dateInputValues[i][0];
if (inputDate != "") { // Exclude blank values
inputDates[inputDate] = true;
}
}
//check and coloring
for (var i = 1; i < datePaymentValues.length; i++) { // Exclude header row
var paymentDate = datePaymentValues[i][0];
if (inputDates[paymentDate]) {
sheet.getRange(i+1, datePaymentColumn).setBackground("#00ff00");
sheet.getRange(i+1, checkResultValueColumn).setValue("check");
}
}
}

Setting a value in a drop down with Google Sheets using a script

I need to figure out a way to set a drop down value based on if another cell in the same row has a value. So for instance if D2 has a value other than '' or null; H2 needs to be set to "New Issue". The answers found in another similar post don't actually work with this since I am using formRedirector. Things got really weird last time. I tried to delete the earlier post but couldn't.
I have some of the logic figured out, but the issue is that what I have now writes "New issue" to H3 and below. Here is what I have so far:
var NEW_ISSUE = 'New Issue';
function defaultValue() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;
var numRows = 900;
var dataRange = sheet.getRange(startRow, 4, numRows);
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var default_status = row[7];
if (default_status != NEW_ISSUE && row == '') {
sheet.getRange(startRow + i, 8).setValue(NEW_ISSUE);
}
}
}
I have a feeling somewhere in the if statement I am messing up somewhere.
This solved the issue, thank you
var NEW_ISSUE ='New Issue';
var row;
var default_status;
function defaultValue() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 1;
var numRows = 900;
var dataRange = sheet.getRange(startRow, 4, numRows);
var data = dataRange.getValues();
var statusRange = sheet.getRange(startRow,8,numRows);
var status = statusRange.getValues();
for (var i = 0; data[i] != ''; ++i)
{
if (status[i] == '')
{
sheet.getRange(i+startRow, 8).setValue(NEW_ISSUE);
}
}
}

Automatically send emails from addresses listed on one sheet when their name is selected from a drop down menu on another sheet

Hi I have a phone enquiry log that has two sheets - I want to automatically send emails to email addresses stored in a second sheet when their name is selected from a drop down box on another sheet.
I would also like to fill column H with a notifacation 'mail sent'but have limited skills as you all can probably see.
This is the code I have so far:
function emailMessage() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var responses = ss.getSheetByName("Enquiry Log");
var lastRow = responses.getLastRow();
var values = responses.getRange("A"+lastRow)+":J"+(lastRow)).getValues(); //get the range and values in one step
var headers = responses.getRange("A1:J1").getValues();
var recipient = responses.getRange("E"+(lastRow)).getValues();
var emailSheet = ss.getSheetByName("Emails");
var names = emailSheet.getRange("A2:A12").getValues();
var emails = emailSheet.getRange("B2:B12").getValues();
var subject = "New phone message";
var message = composeMessage(headers,values);
var email = '';
for (var row=0; row < names.length; row++) {
if (names[row][0] == recipient) {
email = emails[row][0];
break;
MailApp.sendEmail(recipient,subject,message);
}
}
function composeMessage(headers,values) {
var message = 'Please check the Enquiry Log as you have a new message:'
for(var c=0;c<values[0].length;++c){
message+='\n'+headers[0][c]+' : '+values[0][c]
}
return(message);
}
};

Compare value to another spreadsheet using array loop and write new values

Hello all I'm having trouble implementing array loops in my project... Here is what I want to do.
I have a spreadsheet called "Red Book" this sheet gets updated regularly once the staff have updated it I have a column where they can select to submit the data they've just entered on that specific row (editing this column calls an onEdit function).
The data will then be written to another spreadsheet (different file) called "Raw Data"
For each submit I have a unique identifier. I need the onEdit code to do the following...
Iterate through the column A to find the unique identifier
Once found update the data in columns 1 through 5
Below is the script I have so far:
function TransferToAppData(e) {
var destFile = SpreadsheetApp.openById('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
var destSheet = destFile.getSheetByName("Raw App Data");
var ss = e.source;
var s = ss.getActiveSheet();
var uniConstRng = s.getRange("A1");
var uniqueConstVal = uniConstRng.getValue();
var NextOpenRow = destSheet.getLastRow() + 1;
var ActiveRow = e.range.getRow();
Logger.log(ActiveRow);
var uniqueVal = s.getRange(ActiveRow,1).getValue();
var add = s.getRange(ActiveRow,2).getValue();
var name = s.getRange(ActiveRow,3).getValue();
var dt = s.getRange(ActiveRow,5).getValue()
if (uniqueVal == "") {
s.getRange(ActiveRow,1).setValue(uniqueVal + 1);
uniConstRng.setValue(uniqueVal + 1);
var transferVals = s.getRange(ActiveRow,1,1,5).getValues();
Logger.log(transferVals);
destSheet.getRange(NextOpenRow,1,1,5).setValues(transferVals);
destSheet.getRange(NextOpenRow, 6).setValue("Applicant");
}
else {
var destLastRow = destSheet.getLastRow();
var destDataRng = destSheet.getRange(2,1,destLastRow,5)
var destValues = destDataRng.getValues();
var sourceValues = s.getRange(ActiveRow,1,1,5).getValues();
for( var i = 0; i < destValues.length; ++i){
if (destValues([i][0])==uniqueVal) {
for(n=0;n<destValues[0].length;++n){
///I"m stuck!!!
}
}
}
}
}
As you can see I have the first array loop going, but I'm having trouble figuring out how to do a second loop that iterates only on the row where the unique value is found and write the source data to ONLY to row where the unique value was found not the whole sheet.
I figured it out...
Below is the code and here is how it works...
When values in certain columns are edited this code is fired.
1--It finds the unique identifier located in the row which was edited.
2--Compares that identifier with a column of unique identifiers in another spreadsheet.
3--When a match is found it writes the change to the new spreadsheet and exits the loop
function TransferToAppData(e) {
var destFile = SpreadsheetApp.openById('1V3R2RnpA8yXmz_JDZSkBsK9tGR2LjHZp52p5I1CuQvw');
var destSheet = destFile.getSheetByName("Raw App Data");
var ss = e.source;
var s = ss.getActiveSheet();
var uniqueConstRng = s.getRange("A1");
var uniqueConstVal = uniqueConstRng.getValue();
var NextOpenRow = destSheet.getLastRow() + 1;
var ActiveRow = e.range.getRow();
var uniqueVal = s.getRange(ActiveRow,1).getValue();
if (s.getRange(ActiveRow,2).getValue() == "" || s.getRange(ActiveRow,3).getValue()=="" || s.getRange(ActiveRow,4).getValue()=="" || s.getRange(ActiveRow,5).getValue()=="") {
s.getRange(ActiveRow,13).clearContent();
Browser.msgBox("Address, Name, Date Entered & Rent are required fields!");
} else{
if (uniqueVal == "") {
s.getRange(ActiveRow,1).setValue(uniqueConstVal + 1);
uniqueConstRng.setValue(uniqueConstVal + 1);
var transferVals = s.getSheetValues(ActiveRow,1,1,5);
destSheet.getRange(NextOpenRow,1,1,5).setValues(transferVals);
destSheet.getRange(NextOpenRow, 6).setValue("Applicant");
}
else {
var destLastRow = destSheet.getLastRow();
var destValues = destSheet.getSheetValues(2,1,destLastRow,5);
var sourceValues = s.getSheetValues(ActiveRow,1,1,5);
for(var i = 0; i < destValues.length; ++i){
if (destValues[i][0]===uniqueVal) {
destSheet.getRange(i+2,1,1,5).setValues(sourceValues);
break;
}
}
}
s.sort(1,false);
destSheet.sort(1,false);
}
}

Categories

Resources