Retrieving HTML code used in google docs using google app scripts - google-apps-script

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.

Related

Publishing html table on Google Site

I have a html table which is updated very often. Currently we copy and paste the html code to the Google Site as embedded code. That works, but is time consuming and not each editor have the skills to do that.
Now I thought it should be possible to use embed url, but that will not work because the source file is located on a Google Drive ==> only the source code will be shown.
A colleague told me that I can use Google Apps Script (GAS), but it works only when the html table is directly in GAS. That will not work for same reasons as before.
Therefore I tried to get the file, but I receive the the error message
Exception: No HTML file named <style type="text/css">
Here my code
function doGet() {
//getting html file will not work ==> Exception: No HTML file named
var html = DriveApp.getFileById("12fozYxDoK-RDcUIf0Cxpq7Cz8mfdXNjy").getBlob().getDataAsString();
//html file saved as txt ==> Exception: No HTML file named
var txt = DriveApp.getFileById("1Ekiwc7lJPuaelMcSJ9hRtvTsunJ8h1XF").getBlob().getDataAsString();
//return HtmlService.createHtmlOutputFromFile(html);
//return HtmlService.createHtmlOutputFromFile(txt);
//works only with embedded html file
return HtmlService.createHtmlOutputFromFile('DocumentTable');
}
What can I do to show the html table from the Google Drive file?
I figures out how it works:
function doGet() {
var html = DriveApp.getFileById("12fozYxDoK-RDcUIf0Cxpq7Cz8mfdXNjy").getBlob();
return HtmlService.createHtmlOutput(html);
}

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.

How to Convert a simple Google text-only Document to HTML via Google Apps Script? [duplicate]

This question already has answers here:
Get Google Document as HTML
(8 answers)
Closed 1 year ago.
I need to take the contents of a Google Doc and turn it into the HTML code equivalent (for sending as an HTML email), via Google Apps Script.
I can read the file and get the text by cycling thru the elements in the body, but how would one get the formatting (bold, italic) into HTML? Via isBold(offset) and isItalic(offset) and cycling thru each character?
Thanks for any help. ~
This is perfectly doable using a document to compose your message, converting it to html format and using that as an html body in the message.
the code goes like this :
function sendAsHtmlBody(){
var id = DocumentApp.getActiveDocument().getId();
var url = 'https://docs.google.com/feeds/';
var doc = UrlFetchApp.fetch(url+'download/documents/Export?exportFormat=html&format=html&id='+id,
googleOAuth_('docs',url)).getContentText();
MailApp.sendEmail(Session.getEffectiveUser().getEmail(),'testHTML Body','html content',{'htmlBody':doc});
}
function googleOAuth_(name,scope) {
var oAuthConfig = UrlFetchApp.addOAuthService(name);
oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
oAuthConfig.setConsumerKey('anonymous');
oAuthConfig.setConsumerSecret('anonymous');
return {oAuthServiceName:name, oAuthUseToken:"always"};
}
This code will ask for authorization in 2 steps, one for "normal" services (mail, documentApp, ..) and a second for the Oauth function, this latter must be triggered by calling the function from the script editor (if you make a menu and let a new user call that menu it won't work..., you have to use the script editor, see issue 677 here).
And here is a shared document( read only, make a copy to use) to test the code with different fonts, an image and some text.(almost every feature available in Google docs are translated in html... only a few alignment are missing sometimes. I use that very often without noticeable issue)
Is the default sharing option not enough? I know you can select the option to display in email. Just a thought.
You may want to look for another solution, as I am as well. When I go to use this I hget a message stating:
OAuthConfig API is deprecated
Method UrlFetchApp.addOAuthService is deprecated

how can I insert a link, in a Google Drive Document, pointing to a different Doc?

I wan to insert, using Google Apps Script, a link in an existing Document, that opens a different document in the same domain.
I tried with HtmlService, creating a web app, with an hyperlink inside, but, the sanitization process clears the link. Any hint?
You insert links into documents as paragraphs with a link property. For example
function myFunction() {
var doc = DocumentApp.getActiveDocument();
var para = doc.getBody().appendParagraph("this is a link").setLinkUrl("http://www.google.com")
}

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.