Google Scripts Editor - javascript

I want to dynamically manage and update a section of a Google Sites page (think HTML).
I understand this needs to be done via a Gadget (to get it on the page), Google Script, and via HTMLService.
The Google Script Editor has a .gs tab and HTML tab for writing the script and HTML. However, I want to separately manage and update the HTML in a document on Google Drive, and have the script use that to feed the HTML back to the page.
Is there a way to reference the document to .createHTMLOutputFromFile(), so that I can have it use the HTML in the doc on Google Drive, rather than having to compose HTML in the Google Script Editor? Please see .gs function example below:
return HtmlService.createHtmlOutputFromFile
(replace **'Index'** with **'Google Docs URL to Google Docs HTML document'**);
I tried different methods for providing the URL, but the script editor can never find the HTML document using the URL I provide. If this is possible, where can I find an example of how to provide the proper URL?

I'd use HtmlService.createHtmlOutput and stream the text in.

var template_welcome = HtmlService.createTemplateFromFile('welcome_template_view');
return template_welcome.evaluate().setTitle("Template Welcome");
that is an example of how to use an html file that is in your google scripts directory. The welcome_template_view is an html file in my directory.

Related

Open HTML file from a separate spreadsheet

I am trying to make a spreadsheet capable of opening an HTML file as a sidebar, but the HTML file is located on a master spreadsheet. Here is the code that I have already for opening the sidebar using an HTML that is in the spreadsheet.
function sidebar1()
{
var b1 = SpreadsheetApp.getActiveSheet().getRange('C13').getValue();
html = HtmlService.createHtmlOutputFromFile(b1).setTitle(b1);
ui.showSidebar(html);
}
It's only possible if you are the current user of that spreadsheet and that spreadsheet is the container of the script because SpreadsheetApp.getUi() as described in the documentation Returns an instance of the spreadsheet's user-interface environment that allows the script to add features like menus, dialogs, and sidebars. if a spreadsheet is opened by id or url then there is no user interface
I just thought that if you have two spreadsheets already open it might be possible to call a server side function on one to run a server side function on the other using Apps Script API which might be able to launch a Sidebar on the second sheet. So maybe I'm wrong. Give it a try.
One solution would be to open the HTML file into a temporary workbook, and copy the sheet from there into the workbook containing all of them.

Check to see if a user has "edit rights" to a Google Drive URL

I'm trying to figure out how to check a Google Drive URL to see if a specific Gmail account has 'edit rights' to it. Is this possible in Apps Script? If yes, any help is greatly appreciated.
I'm currently researching File.getEditors(), but not sure how to test this code:
// Log the email address of all users who have edit access to a file.
//https://developers.google.com/apps-script/reference/drive/user
var file = DriveApp.getFileById('1mgw9xYO7X99brzrIk4Uel8YzndQa8dpGGQCnrFn4suU');
var editors = file.getEditors();
for (var i = 0; i < editors.length; i++) {
Logger.log(editors[i].getEmail());
}
To test this code, you need to place the code inside a a function inside the Script Editor on Google Drive, then execute the function.
You can access the Script Editor in two ways, either from within a spreadsheet (under Tools->Script Editor), in which case your script will be bound to the Spreadsheet. Binding to a spreadsheet can be useful if you want the script to work with data from sheet, for example you might want to store some configuration settings in the sheet, in which case keeping the sheet and the code together as one file is convenient.
You can also create a stand alone Script by connecting the Google Apps Script app to your Google Drive (+New -> More -> Connect more apps), then creating a stand alone Google Apps Script file directly on drive.
Regardless of how you create the script, the behaviour of this script and the script editor will be exactly the same.
Once you are in the script editor, paste that code into a function (by default you will be given an empty function named "myFunction()". Then run it using the "Run" menu.
Finally, under the "View" menu, go to "Logs" to see the results of Logger.log().

Source of LinkedIn script to create embeddable share widget

I am looking at the docs here:
https://developer.linkedin.com/plugins/share
and I have this:
the problem is that it's referencing a script file:
src="//platform.linkedin.com/in.js"
where/how do I get that script file? I need to include that script file in my project.
The script it in where you typed. platform.linkedin.com/in.js just remove the //. If you are wondering how to download it.
open the url platform.linkedin.com/in.js.
Right click on the code
Click on save page as
save where you want it

Google Apps Script: code to convert a text document in PDF?

I'd like a snipe of code to convert a google docs text document template in a PDF file. I know how to create a new document as a google text document, but not how to transform it in a PDF file.
I find this piece of code:
var pdf = UrlFetchApp.fetch("https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key="+key+"&exportFormat=pdf&gid=1&gridlines=0&printtitle=0&size=7&fzr=true&portrait=1&fitw=1", requestData).getBlob().setName(name);
return pdf;
But I don't need spreadsheet customization (actually, I don't need spreadsheet, but text file).
How can I can do it? Only with UrlFetchApp.fetch class?
One more question: can I automaticaly send this PDF file to be printed after I've created it?
Thanks!
If I understand you correctly your question is about getting a text document as pdf ?
There is a dedicated method for that :
DocsList.getFileById('document ID').getAs('application/pdf');
see related documentation here

How can you retrieve a pages source code (after javascript has ran) using PHP?

On my page, javascript adds a lot of classes on page load (depending on the page).
How can I wait til javascript has added those classes, then get the HTML using either Javascript or PHP from a different file?
When the page has finished loading, POST the rendered source back to a PHP script using Ajax.
$(function()
{
var data = $('body').html();
$.post('/path/to/php/script', data);
});
(This example assumes you're using jQuery)
It looks like what you need is Firebug. If you are using Google Chrome, you could also use the Google Chrome Developer Tools.
These tools will allow you to view the live DOM of the page as well as track any changes made by your javascript. Tools like these are essential to us as developers.
You can not receive the rendered HTML source by an other resource other than from JavaScript on your page itself. After JS finished all the content changes in the HTML, you could post the HTML source to a PHP on the server and save it.
Pseudo code:
// JavaScript using jQuery
setTimeout("jQuery.post('/catch.php', jQuery(document));", 2000);
// on the server side create a catch.php file
<?php
file_put_contents('./tmp.txt', 'php://input');
You can't, easily.
JavaScript modifies the DOM in memory. This is completely separate entity than the "source" you originally sent to the browser.
The closest thing you can do is build an XML representation of the DOM via JS and send it back to the server via AJAX. Why you would want/need to do this is beyond me.
Open your Bookmarks/Favorites and create a new one with this and then click it after your page loads:
javascript:IHtml=document.documentElement.innerHTML;
LThan=String.fromCharCode(60);
LT=new RegExp(LThan,'g');
IHtml=IHtml.replace(LT,'<');
IHtml=IHtml.replace(/ /g,' ');Out ='';Out+='<!DOCTYPE html PUBLIC "-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN"';Out+=' "http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd">';
Out+='<html xmlns="http:\/\/www.w3.org\/1999\/xhtml" xml:lang="en-US" lang="en-US">';
Out+='<head><title>Inner HTML<\/title><\/head>';
Out+='<body style="color:black;background-color:#ffffee;">';
Out+='Body HTML:<br \/><ul>';
NLine=String.fromCharCode(10);
ILines=IHtml.split(NLine);
for (ix1=0; ix1< ILines.length; ix1++) {
Out+='<li>'+ILines[ix1]+'<\/li>';
}
Out+='<\/ul>';
Out+=' [<a href="javascript:void(0);" onclick="window.close();" title="close">Close<\/a>]';
Out+='<\/body><\/html>\n';
PopUp1=window.open('','IHTML');
PopUp1.document.write(Out);
PopUp1.document.close();

Categories