JavaScript Objects - Iterate Through Object Keys and Values - Google Sheets Apps Script - Part 13

preview_player
Показать описание
This tutorial will cover JavaScript Objects and show you how to Iterate through Object Keys and Values, otherwise known as looping through associative arrays with key, value pairs. You will also learn how to add a From Field when sending emails with AppsScripts though Google Sheets.

Google Sheets

Website:
Рекомендации по теме
Комментарии
Автор

I have checked my code a bunch of times and I am still not getting the information from the array to populate into the template. Can you help me figure out where I've gone wrong? Your videos are excellent and I appreciate you sharing so much of your knowledge. Thank you!


Here is my code:
function sendEmails() {



var ss =
var lr = ss.getLastRow();
var templateText = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Template").getRange(1, 1).getValue();

var quotaLeft =

if((lr-1) > quotaLeft) {
Browser.msgBox("You have " + quotaLeft + " messages left in your quota and you are trying to send" + (lr-1) + " emails. Emails were not sent.");
} else {

for (var i = 2;i<=lr;i++){

var currentEmail = ss.getRange(i, 1).getValue();
var currentName = ss.getRange(i, 2).getValue();
var currentClassTitle = ss.getRange(i, 3).getValue();
var currentDate = ss.getRange(i, 4).getDisplayValue();

var info = {
name: currentName,
title: currentClassTitle,
date: currentDate
};

var messageBody = templateEngine(info, templateText);


Logger.log(messageBody);
var subjectLine = "IMPORTANT: About your " + currentClassTitle + " class.";


// MailApp.sendEmail(currentEmail, subjectLine, messageBody);

} // Close For Loop
} // Close Else Statement
}


function templateEngine(obj, templateText) {

var messageBody = templateText;

for(var [key, val] in obj){

messageBody = messageBody.replace("{" + key + "}", val );

}
return messageBody;

}


Here is what I get in the log when I run this:


[20-03-15 10:45:40:304 CST] Hello {name}!
This is a reminder that you have an upcoming {title} class on {date}.


Thank you,
My Name
[20-03-15 10:45:40:319 CST] Hello {name}!
This is a reminder that you have an upcoming {title} class on {date}.


Thank you,
My Name

BigDave
Автор

What I love about your videos is you speak so concisely that I can play your videos at 2x speed and understand what you are explaining. Great job thanks for all the epic videos!

ShizzleMyChizzle
Автор

From @9:27 onwards, use
" for (let [key, val] of Object.entries(info)){ }" instead. The old method doesnt work anymore in 2021

goldylock
Автор

I've done GAS mailmerges for years, and I learned something today.

davidelliott
Автор

Great videos. Really appreciate the time you put into these man you're an internet hero

derekherzog
Автор

Hello Sir this is very useful for me
Thank you

omnarayan
Автор

Sir Your video is really awesome . please make more about java script like how to lookup multiple sheets, query function how to apply filter from any column and filter in same place thanks

vishnutiwari
Автор

man learned a lot from ur videos...thank u very much

muralasiva
Автор

Great video. Any chance of future tutorial about sending e-mails in HTML format?

WilsondotZeroFaustino
Автор

How to save an array-of-objects in sheets while making sure the properties stay in the same column from one row to another?
PS: love your videos, they are AMAZING, keep it up!

mr.nobodyx
Автор

For people watching this,  you need to update the code.

This doesn't work anymore:
for (var [key, val] in obj)

Instead use this:
for (let [key, val] of Object.entries(obj))

Or this, where you get the value by using obj[key]:
for (let key in obj)

tomdevisser
Автор

Hello Teacher,

I have checked and re write my code multiple times but still getting error. Whats my actual error is when I Log only key its gave me correct data but when I loop through and Log val is shows something else.

here is my code

function learn(){
var info = { name: "Bharat", age: 37, gender: "male"};
for (var key in info){
Logger.log(key);
}
} // This works perfect and result is
[21-01-15 10:00:45:121 IST] name
[21-01-15 10:00:45:124 IST] age
[21-01-15 10:00:45:127 IST] gender

now the other one

function learn(){

var info = { name: "Bharat", age: 37, gender: "male"};
for (var [key, val] in info){
Logger.log(key);
Logger.log(val);
}
} // this is also a same code with val and result is

[21-01-15 10:04:43:998 IST] n
[21-01-15 10:04:44:002 IST] a
[21-01-15 10:04:44:004 IST] a
[21-01-15 10:04:44:007 IST] g
[21-01-15 10:04:44:009 IST] g
[21-01-15 10:04:44:011 IST] e

I have re write this for multiple times but still not getting data what it should be.

Please help me here.

Thank you very much for this channel.

nimaanshbusinessautomation
Автор

Thank you for these videos. I've been watching this month in this playlist. I'm from a VBA background and these are helpful.

One question, the idea of assigning data into an object rather than purely processing/replacing it through direct string keywords, in this situation what benefit does it do over the direct replacement if you'll have to code new keys in the object everytime you'll have new data to add anyway? Or is this just for demonstration of objects? I just thought the effort is the same for the direct string replacement without using objects. Thank you.

teediel
Автор

Thanks for the help. Look forward to more!

billeast
Автор

Any suggestion to get the value for {name} twice or more times? It works fine while using it once.

weasleyrichard
Автор

Thanks, how can i get replacements as bold text?

atily
Автор

Sir This code is not working

function learn(){
var info = {
name: "Lisa",
gender: "female",
eyecolor: "blue",
age: 32
};

for(var [key, val] in info){
Logger.log(key);
Logger.log(val);
}
}

output showing like that
[20-07-28 00:09:48:779 IST] n
[20-07-28 00:09:48:781 IST] a
[20-07-28 00:09:48:783 IST] g
[20-07-28 00:09:48:784 IST] e
[20-07-28 00:09:48:787 IST] e
[20-07-28 00:09:48:788 IST] y
[20-07-28 00:09:48:790 IST] a
[20-07-28 00:09:48:792 IST] g

riteshpandey
Автор

I did not understand the last part where you were talking about the “from field “ you entered Argument, passed as an object “name” in the MailApp.sendmail(). Since your template already had Chicago computer Classes at the bottom of the mail, how will this last part of passing the name: “Chicago Computer Classes” benefit or reflect.

ripy
Автор

Thanks, you're helping me a lot ;]

walterpaiva
Автор

Awesome but can you help me with attachments. I want to attach maximum 3 attachments in mail but sometimes it could 2 or 1 but there would always be attachments. how to handle those with this. waiting for your prompt revert.

RahulSharma-ujcr
visit shbcf.ru