I create function get gmail when user use my app in file gs. When i call function via file script html it run but don't get gmail ?
I try test open new web browsers, log in with account gmail and pass URL of apps script but not show my gmail.
Code in file .gs:
function checkAccSS(){
var email = Session.getActiveUser().getEmail();
Logger.log(email); // I test it show my gmail
return email;
}
Code in file .html:
<!DOCTYPE html>
<html>
<head>
</head>
<body onload="doLoadGmail()">
<p id="demo"></p>
<script>
function doLoadGmail(){
google.script.run.withSuccessHandler(checkGmall).checkAccSS();
}
function checkGmall(email){
document.getElementById("demo").innerHTML = "Gmail " + email;// not show gmail in html !
console.log(info); // not show gmail here !
}
</script>
</body>
</html>
As far as I can see this happens because your script 'doLoadGmail()' that executes the script 'checkAccSS' is only called on load. You could add a button with an onclick event that would execute your script without reloading however this wouldn't do it automaticly.
If you would like to this automaticly you could check the documentation from Ajax
https://www.w3schools.com/jquery/jquery_ref_ajax.asp
Related
I'm trying to get the customer to enter the ID of their spreadsheet, so that the remaining script performs the proper functions on the customer's spreadsheet. My code is currently like this:
Script.gs
var id;
var spreadsheet=SpreadsheetApp.openById(id);
function myfunction(x) {
id=x;
}
function doGet() {
var template = HtmlService.createTemplateFromFile('HTML')
var html=template.evaluate();
return html
}
HTML:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<input>ID></input>
<button onclick="passId()">PASS</button>
<script>
function passId() {
google.script.run.myfunction(id);
}
</script>
</body>
</html>
When I try to view the HTML, it returns an error saying that the value of the ID variable is invalid. The intention is for the client to inform the ID of his worksheet, as the rest of the code depends on the ID of the worksheet to work. If I assign the id manually through .gs, the HTML works perfectly in the worksheet with the id assigned, thus applying all the functions present in my code.
EDIT
I made some changes according to the answers, and it looks like this
GS
function logUserInput(x){
var id=x;
Logger.log(id);
return id;
}
function doGet(e) {
var template = HtmlService.createTemplateFromFile('HTML');
template.variablename = "";
var html=template.evaluate();
return html;
}
HTML
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<input id="KK" type="text" value="<?= variablename ?>"/>
<button onclick="passId()">PASS</button>
<script>
function passId() {
google.script.run.logUserInput(document.getElementById("KK").value);
}
</script>
</body>
</html>
Now I will try to assign the value that is inside the "logUserInput(x)" function to a global variable. Thanks!
In Google Apps Script, stuff set out of any function, the global scope, is executed every time that a server side function, including doGet, is ran from the editor, a user interface, including a web application, or by simple/installable triggers.
So first thing that you should do is to remove the following lines from the global scope.
var id;
var spreadsheet=SpreadsheetApp.openById(id);
Below is a simplified implementation of a web app that pass a value entered in a web application created using Google Apps Script to the server side code. Please checkout the comments.
You might try this code or adapt it to your current code.
/**
* Creates the web application using an .html file
*/
function doGet(e){
return HtmlService.createHtmlOutputFromFile('index');
}
/**
* Function called from the client-side. It logs the value
* entered by the web-app user.
*/
function logUserInput(id){
console.log(id);
return `${id} successfully logged`;
}
index.html Contains the web application structure and client-side JavaScript
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<label for="my-input">Enter the spreadsheet id</label><br>
<input id="my-input" type="text"><br>
<button onclick="sendToServer">Save</button>
<script>
/**
* Called by the button click event.
* Sends the input value to the server side and logs the server side
* returned string or the error message if it occurs on the server side
*/
function sendToServer(){
/** Retrieves the input value */
const id = document.querySelector('input').value;
/** Send the input value to the server */
google.script.run
.withSuccessHandler(console.log)
.withFailureHandler(console.error)
.logUserInput(id);
}
</script>
</body>
</html>
References
https://developers.google.com/apps-script/guides/web
https://developers.google.com/apps-script/guides/html/communication
I am trying to automate sending emails on Google sheets using Mailapp.
My problem is the following:
I need to include the gmail signature in the email.
When using Mailapp, the pre-defined signature on gmail does not appear.
Is there any solution for that?
The other solution, as I found online, is using HTML.
I need to develop the email automation for many users on a single google sheet and need to include Linkedin links.
My issue here is that I am not able to assign HTML's 'href' as a variable (variable that is in fact a cell containing the linkedin link.
JS:
var linkedin_link = SpreadsheetApp.getActiveSheet().getRange(1, 1).getValue();
var templ = HtmlService.createTemplateFromFile("MailTemplate");
var message = templ.evaluate().getContent();
MailApp.sendEmail({
to: "xxx#gmail.com",
subject: "Automation",
htmlBody: message,
});
HTML file:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<a href="https://linkedin.com/" + linkedin_link >LINKEDIN</a>
</body>
</html>
If anyone could help it would be great!
Thanks!
If you enable Gmail advance API (under services) you can use this to get the signature as HTML. Then you can use this in your own html template:
function readCurrentOutOfOffice(){
console.log(Gmail.Users.Settings.getVacation('user#company.com'))
}
I have created one portal where I want to configure outlook with the portal. There will be Email button where on click outlook should get open where subject, body and attachment should be added automatically based on data in the database.
I can open outlook and set subject and body but don't know how to add an attachment.
I have checked this link Javascript: Outlook but I am getting ActiveX exception but I believe this is more compatible with IE. Also , Microsoft - Outlook
<html>
<head>
<script>
function myFunction() {
window.location.href = "mailto:test#test.com?subject=TestSubject&body=message%20goes%20here";
}
</script>
</head>
<body>
<button onclick="myFunction()">Open Outlook</button>
</body>
</html>
According to your description, you want to add an attachment with JS, you can use the following code:
mailItem.Attachments.Add("your prfile url");
You need to set “Initializing and scripting ActiveX controls that are not marked as secure” to enabled.
In IE
This is the result of my running:
This question already has answers here:
Is JavaScript supported in an email message?
(11 answers)
Closed 5 years ago.
Can you use JavaScript inside an email, which is sent using Python?
My aim is to send a working clock inside an email.
I am trying to use Python to do so. I am using IDLE and the Python libraries stmplib, email and html2text to send emails. My code looks like the code shown below. I omitted some details (style and script) as they are unimportant.
me = "my.email#gmail.com"
you = "my.email#gmail.com"
msg = MIMEMultipart('alternative')
msg['Subject'] = "Clock"
msg['From'] = me
msg['To'] = you
html = """\
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Clock</title>
<style></style>
</head>
<body>
<div class="clock">
<p class="clockhour">HH</p>
<p class="clocksym1">:</p>
<p class="clockminute">MM</p>
<p class="clocksym2">:</p>
<p class="clocksecond">SS</p>
</div>
<script type="text/javascript"></script>
</body>
</html>
"""
text = html2text(html)
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
msg.attach(part1)
msg.attach(part2)
s = SMTP('smtp.gmail.com:587')
s.ehlo()
s.starttls()
s.login("my.email#gmail.com", "my password")
s.sendmail(me, you, msg.as_string())
s.quit()
The full version of the HTML in this code gives me a properly functioning clock, but if I attempt sending it to myself using Gmail I receive a different result.
Viewing the HTML:
In Gmail:
This shows, that parts of the CSS load, but others like the font or the font size don't. It also shows that the JavaScript does not load. (Normally viewing the HTML code in a browser gives a functioning clock, while in an email doesn't.)
Is there a way to send an email with this clock?
Short Answer: No. You can't use JavaScript for email template.
Tricky way: You can work on relevant file and get the parsed value in your template file using server side language.
Example:
clock.js
// code for js
template.php
// echo the value
// The value is rendered in html and works in email template too.
But this case is not suitable for you as you're trying to implement countdown clock. This is suitable only for static value.
However, linking to external page content will help you to show the timer.
For your case, You may try using http://motionmailapp.com/
Hope, this helps!
javascript is unsupported in email html, but at least you can place a link to the page with clock countdown.
Good morning and thank you in advance for the help. I am a noobi at java/gas scripting so any help is appreciated.
On a google spreadsheet I have a custom menu that launches a small html menu that I would like to be able to launch various web pages from. The actual addresses are variable. I have set those as Keys with the property service when the page launches.
Here is the html code (taken from another example and trying to adapt)"
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<script type="text/javascript">
var myArr = ["Peter", "Paul", "Tony", "Adam"];
function display(name) {
var accScriptPageLinkID= PropertiesService.getScriptProperties().getProperty('scriptPageLinkAdd');
Logger.log(accScriptPageLinkID)
alert(name);
}
for(var i = 0; i < 4; i ++) {
document.write('<input type="button" onclick="display(this)" name="'+ myArr[i] + '" value="'+ myArr[i] +'">'); // Will log Peter, Paul..
}
</script>
</body>
</html>
I need to modify the above code so that when the button Peter is pressed it opens the Script Page linked under the Property Service Key 'scriptPageLinkAdd'.
Or if there is an easier way to create a dynamic html page for my menu that is linked to cells in the google spreadsheet, please advise.
Mike
PropertiesService (and Logger too) are server-side AppsScript classes - they can't be invoked directly from the client side (your html page).
To invoke them on the server side from your html page you must use google.script.run.withSuccessHandler(clientSideFunctionToProcessReturnedData).someServerSideFunction() which can return some data back to your html page.
Learn more about HtmlService and communicating with the server here