replace text in links with function with input parametr URL - google-apps-script

I have a problem with this algorithm in Google Doc script :
I need to go through all the links in Google Doc
get its URL for each link
call the function called "getTextByURL (URL)" with the URL input parameter
and update the displayed link text with the return value of this function.
Used to C#, VBA and Word model, where there is a collection of Hyperlinks,
I have a problem in Google Doc script with this algorithm.
Can anyone advise me?
Thanks, Ilya.

Related

Google Sheets Import XML text value will not display

I'm trying to import some energy data into a Google Sheet, but can't seem to get the value to appear since it is just text in the HTML line, and shows up as --,--- in the output. I have tried adding /text() to the XPath but that is still not working either.
Website with data to import:
http://www.caiso.com/TodaysOutlook/Pages/default.aspx
Formula in Google Sheets:
=IMPORTXML("http://www.caiso.com/TodaysOutlook/Pages/default.aspx","//div[#class='overview-large-number']")
Also tried the follwoing Xpath with the same --,--- being displayed.
/html/body/div[2]/div[3]/div[4]/div/div/div[2]/div/div[1]/div[1]/div[1]
/html/body/div[2]/div[3]/div[4]/div/div/div[2]/div/div[1]/div[1]/div[1]/text()
Screen Shot of Webpage:
Caiso Webage with Data
HTML Element with Data:
<div class="overview-large-number">41,946 <span>MW</span></div>
Screen Shote of Google Sheets current Output:
Google Sheets Screenshot
Thank you in advanced for any insight.
The data is populated after the page loads, so you can't use ImportXML(). The data is requested via HTTP Get on the URL http://www.caiso.com/outlook/SP/stats.txt and is returned in JSON format.
Get the ImportJSON script from here and add it to your sheet (Tools > Script Editor).
This should be enough to get you started:
=Transpose(ArrayFormula(HLOOKUP(
{"AvailableCapacity","CurrentSystemDemand","todayForecastPeakDemand","histDemand","TodaysPeakDemand","tomorrowsForecastPeakDemand"},
ImportJSON("http://www.caiso.com/outlook/SP/stats.txt"),2,0)))
Note that the Historical Peak value is already a string, so you may have to do some extra work to get the number out.

email google spreadsheet as html

I am trying to create a google script that emails out a spreadsheet as html. I am trying to convert the spreadsheet to html using the export url, but currently google docs only lets you export it out as a zip. Is there a way to get the html representation of a spreadsheet worksheet?
function getDocAsHtml(docId){
var url="https://docs.google.com/spreadsheets/d/" + docId + "/exportFormat?format=html";
var fetch=UrlFetchApp.fetch(url+docId).get
return fetch;
}
Publish the sheet that you want to get the HTML out of:
File Menu, PUBLISH TO WEB
Make sure that: "Automatically Republish When Changes Are Made" is checked.
Get the URL of the published page. Use that URL in a UrlFetchApp.fetch() request.
Use UrlFetchApp to get the content of that published sheet.
function fncSheetToHTML() {
var theSheetContents = UrlFetchApp.fetch("https://docs.google.com/spreadsheets/d/YourID_Here/pubhtml?gid=123abc&single=true");
Logger.log("theSheetContents: " + theSheetContents);
}
The returned contents is a string. If you view the LOGS of what is returned from the above code, you'll see HTML tags in the content.
The published sheet is visible to anyone who uses that URL. So, if you don't want people to see the contents, this method might not be what you want. If you don't share that published URL, I don't know how likely it is that anyone will ever find it.

Retrieving HTML code used in google docs using google app scripts

Is it possible to use Google app scripts to get the html code in Google docs i.e if text in the document was say bold,i could get the html code for that.
What i have so far :
function getHtml(id) {
var doc1=DocsListExtended.getFileById("1ta7zJ6SDFgzgp-UprjehxR8Tx3-4-wtJwTYqWbol1SU").getAsHTML();
var body=HtmlService.createHtmlOutput(doc1);
var cont=body.getContent();
return cont;
Logger.log(cont);
}
I don't know a simple way for that. You can navigate the elements of the document using DocumentApp API, and converting it block by block to HTML.
I already tried
DocumentApp.getActiveDocument().getAs(MimeType.HTML).getDataAsString()
but the error says doc can't be converted to html mimetype.

google site: pass parameters to embedded 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~~

How to pass parameters to Google Apps Script Gadget embedded in a Sites Page?

I have a Google Apps Script gadget that is embedded in a Google Sites page. I would like to pass the gadget a page parameter, such that when the page is opened with URL like:
https://sites.google.com/a/mydomain/mysite/mypage?myparameter=1
I can access the value of the page parameter in the Apps Script gadget code like so:
function doGet(e) {
var app = UiApp.createApplication();
app.add(app.loadComponent("MyComponent"));
var myparam = e.parameter.myparameter;
return app;
}
Currently, the value of e.parameter.myparameter is coming back as null. Is there a way to setup my Apps Script to support this? Any approaches are welcome.
Maybe the link bellow will help you - I have not tried it myself yet...but I will try it out in the next days.
http://code.google.com/p/google-apps-script-issues/issues/detail?id=535
I posted this on the code.google.com page linked in the accepted answer, but the way to have parameters passed through to Apps Script is by adding "https://sites.google.com/feeds" to the Apps Script scope. (See this site for information about how to add explicit scopes.) Once this scope is added, the following works:
in Code.gs:
function doGet(e) {
var htmlTemplate = HtmlService.createTemplateFromFile("page");
htmlTemplate.urlParams = e.parameters;
return htmlTemplate.evaluate();
}
in page.html:
...
<head>
...
<script>urlParams = <?!= JSON.stringify(urlParams) ?>;</script>
</head>
...
urlParams is now available as a variable in your JS code in the following form:
urlParams = { key1: [value1, value2, ...], key2: [value1, value2] }
This example describes a parameter as "&name=value" however I have not been able to get it working in either a Google Apps Site, or in a Personal Google Site. (which seem to handle authentication in different ways)
The example seems to work fine when I hard-code the value so maybe I am just not parsing it right or something, I will try to follow up here when I understand this better but I also have not yet found adequate explanations of these features.
One Hypothesis is that Google changed something, I note the menu structure does not seem to match what I assume it use to be since I see many references to a [share] button/menu.