Google Apps Script, Javascript, strings
Hey there, Yagi here, you’ve probably stumbled across this page from a link from one of my other in-depth tutorials. This is just a quick primer on Javascript Strings in Google Apps Script for the non-coder.
Here’s a bare-bones example of how a string of text might come together in your code:
|
function string(){ var meatz = ["chicken", "liver"]; var message = "\nI like "+ meatz[0] +".\n"+ "I like "+ meatz[1] +". \n"+ "Meow Cat, Meow Cat "+ "They deliver." Logger.log(message); }; |
The resulting log would look like this:
Strings, +, \n and arrays[value]
If you are unfamiliar with basic string syntax and joining (concatenation), basically you can write text inside a single (‘) or double(“) quotation marks. For example:
|
"This is a string" 'This is also a string' |
To join two strings together you can concatenate them with a plus sign (+).
|
"I like chicken."+ "I like liver. "+ "Meow Cat, Meow Cat "+ "They deliver." |
Just remember to put a space inside your quotation mars at the end if you are going to join another word or the words will not be spaced and theywillbealltogether.
This is handy when you want to put things on separate lines in your code but it won’t be on a separate line when it is displayed. You can use the newline escape sequence \n
to return the next part of a string to a new line.
|
"I like chicken.\n"+ "I like liver. \n"+ "Meow Cat, Meow Cat "+ "They deliver." Displays: I like chicken. I like liver. Meow Cat, Meow Cat. They deliver. |
To insert a custom variable into a string, one fairly beginner-friendly approach is to put it between two plus signs.
|
var meatz = ["chicken", "liver"]; "I like "+ meatz[0] +".\n"+ "I like "+ meatz[1] +". \n"+ "Meow Cat, Meow Cat "+ "They deliver." Displays: I like chicken. I like liver. Meow Cat, Meow Cat. They deliver. |
You can see in the meatz
variable above, we have put our two meat items in an array. An array always starts at zero (0). So to get chicken we would do, meatz[0]
.
Meow get on back to your main tutorial and get learn’n.
Need help with Google Workspace development?
Go something to solve bigger than Chat GPT?
I can help you with all of your Google Workspace development needs, from custom app development to integrations and security. I have a proven track record of success in helping businesses of all sizes get the most out of Google Workspace.
Schedule a free consultation today to discuss your needs and get started or learn more about our services here.
~Yagi
Like this:
Like Loading...