google site: pass parameters to embedded script - google-apps-script

I am trying to pass parameters to an app script embedded in page on google sites. This page was created using a custom template that has the original script. Everything works on the template but on the page itself, i get an 'undefined' when I try to use the values.
I am using the usual e.parameter.name code in the doGet(e) to get the value for the parameter 'name'. I do not understand why it works for the template but not for the page with the same template.

You cant pass parameters to the script from the embedded script url. Gas only picks up parameters from sites if those are in the site url itself.
As a workarround you can embed the gas url (with params) using the iframe gadget instead of inserting the script the normal way.

Try using parameters:
function doGet(e) {
Logger.log(e.parameters.name[0]);
}

Make sure you use the right link as well. Especially in an iframe in Google Sites. This one works for me, which changes macro.google.com to sites.google.com and makes other changes as well:
https://sites.google.com/macros/s/~~script ID~~/exec?param1=~~passed value~~&param2~~passed~~

Related

Inject HTML signature into Gmail from hosted page

I have the basic shell of a Chrome extension done and have come to the point where I am trying to inject an HTML signature into Gmail using code hosted on an unindexed page on my site. The reason I want to do this is to be able to include web fonts, something that for the life me I can't figure out why Gmail hasn't allowed you to do from their font library.
In any regard, as I said, I have a right-click context menu option ready to trigger a script from my js function page and the extension loads without errors. I need to figure out the best way to inject the HTML into the email and without losing any of the formatting that has been done on the page.
I have created the extension manifest, set the permissions on the context menu and created a function to call back to the js page that will inject the signature.
var contextMenus = {};
contextMenus.createSignature =
chrome.contextMenus.create(
{"title": "Inject Signature",
"contexts": ["editable"]},
function (){
if(chrome.runtime.lastError){
console.error(chrome.runtime.lastError.message);
}
}
);
chrome.contextMenus.onClicked.addListener(contextMenuHandler);
function contextMenuHandler(info, tab){
if(info.menuItemId===contextMenus.createSignature){
chrome.tabs.executeScript({
file: 'js/signature.js'
});
}
}
The end result is nothing enters the page and get massive errors related to cross-site because the domain is not the same obviously. This has obviously been solved as there are numerous signature extensions out there. I would probably use one of theirs but a) I want to build it on my own, b) they all want you to use their templates, none of them that I have seen will let you just use your own code.
So, any ideas?

How do I get the base URL in web app (i.e., /exec vs. /dev)?

How do I get the base URL from within the doGet(e) function?
If I am writing an HTML template that is setting anchors, how do I determine the base URL? It could be .../exec or .../dev.
I do not see the URL in the parameters.
techhowch's comment got me going in the right direction.
ScriptApp.getService().getUrl() does the trick!
Documentation:
https://developers.google.com/apps-script/reference/script/service#getUrl()
https://developers.google.com/apps-script/guides/html/reference/host#properties

How can I get url parameters from a google sites page

For a Google site, I want a page to display content based on the url parameters.
Eg. http://sites.google.com/../mypage?id=123
Then I want to make a HTTP request using the id and display the result on the page.
Or I want to use App Scripts to perform something and display the result on the page.
How can I do so?
Use on Google app script
function doGet(e){
// id in your Url. example : http://sites.google.com/../mypage?myidok=123
var element - e.parameters.myidok
// code
// your app
}
See this answer for an example of serving multiple html pages using HtmlService. The basic idea is to write doGet() to accept a query parameter that it will use to select which html page to serve.

doPost stopped working google app script

I have had an HTML Service script that has worked for at least 8 months that uses a doGet for the first page and doPost of all subsequent pages.
Yesterday, when I loaded the page, the doGet works fine, pulls info from a spreadsheet and renders the the form. Regardless of what selections I make, the submit button does nothing.
I then went back to my personal gmail account, unrelated to the production account, untouched and using private dummy copies of data, where I originally coded it and that one no longer works.
I looked in the execution transcripts and it shows proper html rendering to the bottom of the first page, but nothing else.
Has doPost been retired?
I've had a working script fail recently, the fix was to force the Sandbox emulation mode to EMULATED.
The default mode WAS emulated with a caveat in the docs saying this could change.
Without seeing your code I'm not sure if that is relevant or not....
function doGet() {
var html = HtmlService.createTemplateFromFile("FormHtml");
html = html.evaluate();
html.setSandboxMode(HtmlService.SandboxMode.EMULATED);
return html;
}

Embedding Google drive in a Google page

I am trying to show a specific privately shared drive folder in my Google page. I am using this guide: https://developers.google.com/apps-script/articles/embedding_docslist_in_sites
I am stuck on Step "8. Replace PASTE_PAGE_URL_HERE in the method getPageByUrl() by the url of your file cabinet."
I have tried every URL combination in "" and I just get a scripting error on the getPageByUrl() method.
I just tested this and it seems to work fine for me. Make sure you are using the full URL for your file cabinet page. For example, on my test site, I created a new page using 'file cabinet' in the template dropdown calling it "myfcpage". This gives me a new page with URL: "https://sites.google.com/site/mytestsite/myfcpage". Therefore my function call looks like this:
var page = SitesApp.getPageByUrl("https://sites.google.com/site/mytestsite/myfcpage");