I am new to using Google Script, but it seems like a fascinating interface with the Google Doc, Calendar, and Mail software that I look forward to using!
Our group has a website that I would love to embellish with dynamic Javascript components that access Google Docs. What I hoped to do was create Functions in Google Script and call to them in the HTML of our website.
So, I made a Google Script page with two functions:
function getName(EMT_ID) {
// ...
};
function getStrikes(EMT_ID) {
// ...
};
These get data from a Google spreadsheet that we use. I won't include the details because these work fine by themselves when accessed through Google Script testing environment.
I then published this as a "Web App" so that I can gain access to these functions from other platforms.
Now, on our other webpage, I added this code (exact title removed)
<script src="https://script.google.com/macros/s/AKfy[...]65K0/exec"></script>
<script>
function strikes()
{
var EMT_ID = document.getElementById('USC ID').value
document.getElementByID('output').InnerHTML = getName(EMT_ID) + ' has ' + getStrikes(EMT_ID) + ' strikes.'
}
</script>
<input type="text" id="USC ID">
<input type="button" onclick='strikes()' value='Check Strikes'>
<p>Output here:</p><p id="output"></p>
It was my hope that the would allow me access to the Javascript functions from the Google Script page. Is this the case? If not - how else should I go about this.
Best Regards
--- EDIT
To add details - the webpage simply does not seem to respond at all to pressing the button.
Google Apps Script functions are not directly callable from external javascript in the way that you've tried. When you published your script as a web app, you were provide a URL that the app was accessible at, via HTML. It didn't expose the functions of your script.
You do have some options, though.
You can create HTML within your google script using the HTML Service, and THAT html can utilize your apps-script functions indirectly via templated HTML or directly like this:
<script>
google.script.run.doSomething();
</script>
You can use the Content Service to have your Apps Script provide feeds (RSS, JSON, JSONP).
Your published service can be written using the UI Service, with full access to your other apps script functions, forms, charts, etc.
There are variations on the above themes as well - this list isn't exhaustive.
Related
I would like to include a dynamic (leaflet-) map on my homepage. Before the map is shown and background data is loaded from external sources (e.g. OpenStreetMap), the users should explicitly agree to sending their meta data (IP, BrowserType etc.) to that external web pages.
The JavaScript library iframemanger allows something similar if the content to show is completly provided by an external url, e.g.
https://orestbida.com/demo-projects/iframemanager/demo1/
However, instead of specifying a service url, I would like to define the deferred content on the same page. I need to somehow define, how my map should look like and what layers it should contain. It would also be great, if that content could interact with the rest of the page, for example react to changes of user selections. That does not work with iframemanager.
If I would use iframemanger and put the content on an extra page, I would again have the problem, that that extra page is not in compliance with GDPR.
=> Is there an alternative to iframemanager, where the content is defined on the same page?
=> If not, could you get me started to implement an own solution for it?
The solution should support:
Three Options
a) "Accept",
b) "Accept and remember decision"
c) "Undo of remember decision" (e.g. by extra check box below content or by deleting cookies of page)
Preview via custom specified image file
Related:
a) I could find some GDPR JavaScript libraries, but they seem to serve a different purpose:
https://github.com/wimagguc/jquery-eu-cookie-law-popup
https://github.com/zoxxx/GDPR-cookie-consent
https://github.com/osano/cookieconsent/
https://www.npmjs.com/package/react-gdpr-consent
b) A different strategy would be, to use my server as a proxy to load external data. However, I would prefer a purely client based solution, that does not need an extra server component.
https://dr-dsgvo.de/datenschutzfreundliche-interaktive-karte-fuer-webseiten-plugin-zum-download-als-google-maps-ersatz/
https://github.com/heiseonline/embetty
c) Related questions:
leaflet.js gdpr compliant integration
Is it possible to implement GDPR Cookie Consent with pure CSS/HTML and no JavaScript? Don't want script blockers blocking it
How do you disable <script> elements using JavaScript
Edit
Here is some first draft (without remember option cookie). It shows a preview image "preview.png" with a button and loads a script "deferred_action.js" after a user clicks on the button "Load map!".
<div
id="disclaimer-container"
style="position:relative;"
>
<img
src="preview.png"
style="width:800px"
></img>
<button
class="accept-button"
onclick="loadScript('deferred_action.js')"
style="position: absolute;left:400px; top:200px"
>
Load map!
</button>
</div>
<script>
function loadScript(script_path){
var script = document.createElement('script');
script.src = script_path;
script.onload = () => {
var disclaimerContainer = document.getElementById('disclaimer-container');
disclaimerContainer.style.display = 'none';
};
document.body.appendChild(script);
}
</script>
Say I have a page like this:
textarea {width:300px;height:200px}
button {display:block}
<textarea value="f">id,value
2,alpha
3,beta
14,test</textarea>
<button>Open in Google Sheet</button>
I want the user to click the button "Open in Google Sheet" and open the csv as a spreadsheet.
I saw that Google Analytics and some other Google products have this button. But I didn't find 3rdparty webapps have this. Is that possible for me to use it?
I believe your goal is as follows.
From I want the user to click the button "Open in Google Sheet" and open the CSV as a spreadsheet., you want to retrieve the text value from the textarea tab and create a Google Spreadsheet using the text value, and then, want to open the Google Spreadsheet.
In order to achieve your goal, how about the following flow?
Retrieve the text value from the textarea tab.
Send the text value to Web Apps created by Google Apps Script.
At Web Apps, a new Google Spreadsheet is created and the text value is put to the sheet.
In order to open the created Spreadsheet, change the permission of the Spreadsheet. In this case, it is publicly shared as the read-only. This is the sample situation.
Return the URL of the Spreadsheet.
When this flow is reflected in the script, it becomes as follows.
Usage:
1. Create a new project of Google Apps Script.
Sample script of Web Apps is a Google Apps Script. So please create a project of Google Apps Script.
If you want to directly create it, please access https://script.new/. In this case, if you are not logged in to Google, the log-in screen is opened. So please log in to Google. By this, the script editor of Google Apps Script is opened.
2. Sample script.
Please copy and paste the following script to the created Google Apps Script project and save it. This script is used for Web Apps. In this sample, the value is sent as the POST request.
function doPost(e) {
const csv = Utilities.parseCsv(e.postData.contents);
const ss = SpreadsheetApp.create("sample");
ss.getSheets()[0].getRange(1, 1, csv.length, csv[0].length).setValues(csv);
DriveApp.getFileById(ss.getId()).setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);
return ContentService.createTextOutput(ss.getUrl());
}
3. Deploy Web Apps.
The detailed information can be seen at the official document.
On the script editor, at the top right of the script editor, please click "click Deploy" -> "New deployment".
Please click "Select type" -> "Web App".
Please input the information about the Web App in the fields under "Deployment configuration".
Please select "Me" for "Execute as".
This is the importance of this workaround.
Please select "Anyone" for "Who has access".
In this case, the user is not required to use the access token. So please use this as a test case.
Of course, you can also access to your Web Apps using the access token. Please check this report.
Please click "Deploy" button.
Copy the URL of the Web App. It's like https://script.google.com/macros/s/###/exec.
When you modified the Google Apps Script, please modify the deployment as a new version. By this, the modified script is reflected in Web Apps. Please be careful this.
You can see the detail of this in the report of "Redeploying Web Apps without Changing URL of Web Apps for new IDE".
4. Testing.
As the test of this Web Apps, I modified your script as follows. Before you use this script, please set the URL of your Web Apps to url. When you open this HTML and click the button, a new Spreadsheet including the text value in the textarea tab is opened with new window as the read-only.
<textarea id="sampletext" value="f">id,value
2,alpha
3,beta
14,test</textarea>
<button onclick="sample()">Open in Google Sheet</button>
<script>
function sample() {
const url = "https://script.google.com/macros/s/###/exec"; // Please set the URL of your Web Apps.
fetch(url, { method: "POST", body: document.getElementById("sampletext").value })
.then((res) => res.text())
.then((url) => window.open(url, "_blank"));
}
</script>
Note:
When you modified the Google Apps Script, please modify the deployment as a new version. By this, the modified script is reflected in Web Apps. Please be careful this.
You can see the detail of this in the report of "Redeploying Web Apps without Changing URL of Web Apps for new IDE".
My proposed script is a simple script. So please modify it for your actual situation.
References:
Web Apps
Taking advantage of Web Apps with Google Apps Script
Looking into using Google Cloud Print, it seems that it is quite complicated regarding OAuth2, the various tokens/client ids etc.
What is the simplest possible way to print a PDF from a web page?
Implemented client side in Javascript with AJAX (so with CORS) or server side with Java (but preferrably not too many jars needed)
PDF document can be sent as binary or referred to as publicly available URL
Preferrably no user login, must be with some kind of "service" authorization
The same application is already using API keys for google maps geocoding. So re-using these keys, if possible, would be the ideal option.
It would be great with some pointers on how to do this in the simplest possible manner possible.
The simplest possible scenario is using the GCP Web Element, as described in: https://developers.google.com/cloud-print/docs/gadget
It boils down to including the print gadget scripts, creating a container to host the button and creating the print gadget in it:
<html>
<head>
</head>
<body>
<div id="print_button_container"></div>
<script src="https://www.google.com/cloudprint/client/cpgadget.js">
</script>
<script>
window.onload = function() {
var gadget = new cloudprint.Gadget();
gadget.setPrintButton(
cloudprint.Gadget.createDefaultPrintButton("print_button_container")); // div id to contain the button
gadget.setPrintDocument("url", "Test Page", "https://www.google.com/landing/cloudprint/testpage.pdf");
}
</script>
</body>
</html>
If you are not logged-in your GCP account you will be shown the appropriate log-in dialog and then you'll select the target printer.
Check the fiddle here:
https://jsfiddle.net/0ncsuqra/
I have a web app with Google Apps Script and would like to take a URL parameter and use it in modifying my HTML via Javascript, but am finding this tricky.
If I try using window.location in my Javascript it gives a different URL than the one shown in the address bar. The URL shown in the address bar is like this ... https://script.google.com/macros/s/MY_SCRIPT_ID/exec?param1=value1 .... but window.location gives something like this https://SOME_SORT_OF_LONG_ID-script.googleusercontent.com/userCodeAppPanel (it doesn't have param1 / value1 at all).
I know how to get the parameter value when I'm in the doGet(e) function -- by using e.parameter.param1 -- but I don't know how to be able to then subsequently use that value in some Javascript.
Help, please!
The html that GAS provides is never the actual URL, it is essentially another ID that google uses to keep track of its web pages. Remember that all Google apps are running on the Google server.
This may not be the same with a standalone script, but I suspect it will be, but I know if you get a google doc, the actual URL is:
https://docs.google.com/document/d/{{{ Your Document ID }}}
I expect a standalone app will be similar. Try using your webapp.getId(), and then adding it to the actual url of your script.
I need a way to use a Java API (jar) from the javascript code on the local client. Can this be achieved and how?
Context
I have a Java API (jar file) that allows to connect to a real time information feed. You can submit a query and, for example, print the events you will receive:
service.subscribe(query, evt -> print(evt));
That API can only be used on the client machine for legal reasons so I can't expose it as a web service from a server.
Goal
I would like to create a web page that gets data from a web service and combines it with the real time information data obtained from the Java API locally.
I am using angular 2 but happy to consider any suggestions.
Web service
I have seen various similar questions but the answers tend to be: expose the API via a web service - that is not possible in my case.
You can use java applets for this purpose.
You should start by making an applet that encloses the call to your method:
public class TestApplet extends Applet{
private ? service = ...;
public Object subscribe(Object query) {
return service.subscribe(query, evt -> print(evt));;
}
}
This applet can then be included in the html of the webpage:
<script src="https://www.java.com/js/deployJava.js"></script>
<script>
<!-- applet id can be used to get a reference to the applet object -->
var attributes = { id:'testApplet', code:'yourpackage.TestApplet', width:1, height:1} ;
var parameters = {jnlp_href: 'test_applet.jnlp'} ;
deployJava.runApplet(attributes, parameters, '1.6');
</script>
Then you can use javascript to call the methods:
var greeting = testApplet.subscribe("Test");
Note that applets are being phased out because of their security problems, but this is ok in an controlled and embedded environment.
The following oracle tutorial gives more information about this technique: Invoking Applet Methods From JavaScript Code
The only way I can think of is using the javafx webview: http://docs.oracle.com/javafx/2/webview/jfxpub-webview.htm
Basically:
you create your own java-based-"webbrowser" withjavafx
you can then expose the java api into the webview
you can open a normal html page (i.e. http://server.tld/mypage.html) within the webview and use javascript to access the api
in the javascript you can check if the site has been opened with a normal browser or with you custom webview by checking if the exposed api is available:
the java code for something like that:
WebView webView = new WebView();
jfxPanel.setScene(new Scene(webView));
webEngine = webView.getEngine();
webEngine.setJavaScriptEnabled(true);
webEngine.setConfirmHandler(new ModalConfirmDialog(self));
// get the window and pass the required daos
JSObject jsobj = (JSObject) webEngine.executeScript("window");
// pass the dataaccess to the js context
jsobj.setMember("javaapi", getApiInstance());
webEngine.load("http://whatever.tld/mypage.html");
in javascript:
if(!window.javaapi) {
alert("Unable to get local java api");
return;
}
Other possibilities:
Applets: they wont work because they need to be downloaded from the same source as the webpage (which you cant use because of licensing restrictions)
JSP/Servlet: cant be used because this means the api must reside on the server (again licensing restriction)
Java Javascript Engine: You can call javascript directly from java, but since you want the javascript in a webpage, this wont work either...
Simply you cannot run .jar file from java script (But you can execute from nodejs) You can use applet to do that. You can refer this link.
It might sound really weird but actually, there's such tool that enable's you to 'convert' from java to js. But of course it has it's limitations and in order to successfully apply it in your particular case w/o doing modifications and dancing with a tambourine, you should be extremely lucky. this tool is able to convert it to js and allows you to create a JS API for the converted JS, so that it can be accessible from other js scripts. What I'm talking about is GWT. You must have source files of a jar (it might be decompiled sources) including all sources of dependencies that are used by lib.
Maybe you can invest in building a bridge between your JS code and the jar using Nashorn.
It allows you to evaluate JS code and invoke JS functions from Java, so it may serve your usecase. You can build a Java layer to connect to the API from the jar and then publish the results to JS by calling some function using Nashorn.
Or you can make use of the ability to directly call Java functions from JS.
Here is a simple tutorial