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
Related
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'))
}
There are two files: 1)app.js and 2) browser.html. app.js just stores a value in a node-js local storage and browser.html should alerts that. But browser.html alerts "null".
My JavaScript code:
var LocalStorage = require('node-localstorage').LocalStorage;
localStorage = new LocalStorage('./scratch');
var a="salam";
global.a=a;
localStorage.setItem('a',a);
My html code:
<!DOCTYPE html>
<html lang="en">
<head>
<title>TEST</title>
</head>
<body>
<input value="Smart contract data" max="25" id="12"></input>
<script src='js/app.js'> </script>
<script>
alert(localStorage.getItem('a'));
</script>
</body>
</html>
What is the problem and its solution? Is there any way to communicate between node-js storage and a html file? Please guide a beginner man.
The issue here is that node-localstorage is entirely backend, and htlm5 localStorage is entirely client side. You will not be able to pull backend values set with node-localstorage on the client end.
In order to pass values from node to the client you will need a template engine. Express provides a pretty complete list of those engines here:
https://expressjs.com/en/resources/template-engines.html
I would like to use data from my google sheet and have it displayed on my google site but not be embedded, just plain text so that the data can be made part of a sentence (eg, the bold bits in this sentence "We have 35 new lines this week and 12 of them are on magazines" ).
I have looked at other post and these two posts come close but as they are old, the info doesn't quite work anymore or are just not fully answered.
How do I grab data from only 1 cell and input it into HTML?
How do you Import data from a Google Spreadsheet to Javascript?
If anyone could help or even point me to maybe another topic which I have missed which answers this question that would be great, thanks in advance for the help.
You can create an HTML result in apps script see: HTML service and just add the result as an iFrame in your google site.
Here you have a more complete example of the use of HTML Service ctrlq.org
Here is a short example from the docs:
gsCode:
function doGet() {
return HtmlService
.createTemplateFromFile('Index')
.evaluate();
}
function getData() {
return SpreadsheetApp
.openById('[spreadsheet_ID]')
.getActiveSheet()
.getDataRange()
.getValues();
}
HTML:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<table>
<? for (var i = 0; i < data.length; i++) { ?>
<tr>
<? for (var j = 0; j < data[i].length; j++) { ?>
<td><?= data[i][j] ?></td>
<? } ?>
</tr>
<? } ?>
</table>
</body>
</html>
This is a general idea for your request rather than direct answer:
Create a Google App Script in your Google Site Setting Page
Within the Script, use SpreadsheetApp Class to read the data from your Google Sheet into array
Within the script, use HTML service to output your array as texts.
Deploy the Google App Script as Web App
Back to your page, insert your Script.
It will work and you can customise the dynamic contents you want to display but you will need to learn Google Script (just javascript with their apis) and code it out yourself.
Some References:
https://developers.google.com/apps-script/guides/sheets
https://developers.google.com/apps-script/guides/web
I have an argument in JS which holds pretty much the data. It's the server information to my server. It changes often, e.g 20/64 or 32/64. You get the point.
I am trying to get the contents of the data to go on an external site, however, when I try, it doesn't work.
To summerise, I have a div which holds the data, I want to get that data using JS and put it on an external site which isn't using the same domain or web server.
HTML FILE:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<script src="jquery.js" type="text/javascript"></script>
</head>
<body>
<div id="serverstats-wrapper"></div>
<script src="import.js"></script>
</body>
JS File:
$(document).ready(function(){
$.post("query.php", {},
function (data) {
$('#serverstats-wrapper').html (data);
});
});
var the_main = document.getElementById("serverstats-wrapper");
var the_data = the_main.textContent ? the_main.textContent : the_main.innerText;
I want to get the text from the html file to the js file then take it to an external website.
Tasid! This won't work! JS does't have such a technique implementet. To do so, you need node.js. This allows you to send the data over a socket to your other webserver.
It does't work difrently, because JS is executed direct on your PC.
You can grab data from another site; but you cannot inject JS code into another site. Here are some methods to retrieve html from another site: Include another HTML file in a HTML file
Update
I found 'webview.executeScript' however, I can't access any of the custom functions.
This is a major step forward though. Any suggestions welcome.
Original post
We developed a Kiosk Web Extension for Chrome OS that loads an externally hosted webpage. The page is loaded into a webview. This page display dynamic information that gets updated daily. In order to push "manual" updates out to the page, we maintain an open WebSocket connection to the Kiosk app. This allows us to refresh the page, update the page, push information, etc. However, we need to access either the javascript loaded inside the webview or we need to access the elements. I'm not sure if I'm explaining this right, or if it is possible to do what I am asking.
Right now we are opening two connections to the websocket server. One is from the webpage and the other is from the extension. The goal is to cut down to one connection.
I added pseudo code to give an idea of what is I need to do.
Chrome Web Extension Kiosk index.html
<html>
<head>
<title></title>
</head>
<body>
<div id="page">
</div>
</body>
</html>
Kiosk example background js
(note: I know this wouldn't be doable via jquery)
document.onLoad({
$("#page").html('<webview id="browser" src="example.com"></webview>');
});
function ReceiveWebSocketMessage(msg) {
switch (msg.cmd) {
case "updatedate":
$("browser").UpdateDate(msg.data);
break;
case "reboot":
chrome.runtime.RebootDevice();
break;
}
}
Webpage example.com
<html>
<head>
<title></title>
</head>
<body>
<div id="datetime">
1/1/2016
</div>
</body>
</html>
Webpage example JS http://example.com/app.js
function UpdateDate(newdate) {
$("#datetime").html = newdate;
}
I threw together a quick diagram of what the old design is like and what the new design is.
To sum it up, I need to access functions that are on the external page. I've not had luck trying to accomplish it.
Resolved! I found the following examples:
https://github.com/GoogleChrome/chrome-app-samples/tree/master/samples/webview-samples/shared-script
Basically I added this to my code:
function Execute(code) {
var webview = document.querySelector('webview');
webview.executeScript({
code: generateScriptText(code)
});
}
function generateScriptText(fn) {
var fnText = fn.toString()
.replace(/"/g, '\\"') // Escape double-quotes.
.replace(/(\r?\n|\r)/g, '\\n'); // Insert newlines correctly.
var scriptText =
'(function() {\n' +
' var script = document.createElement("script");\n' +
' script.innerHTML = "(function() { (' + fnText + ')(); })()" \n' +
' document.body.appendChild(script);\n' +
'})()';
return scriptText;
}