How to copy html response in clipboard automatically - html

I want to copy html response in clipboard automatically without clicking on button. I have tried but not succeeded like using javascript
var textarea = $("#Data");
textarea.select();
var successful = document.execCommand('copy');
But it returns false..
How to proceed?
I have read previous posts but it not helpful

Related

Dynamically changing URL's in an HTML File - Google Apps Scripts / Google Sheets

I have written a script that creates a new "Report" every time it is ran. This is done by generating a new Google Spreadsheet, which then displays a filtered set of values based off inputs in the original spreadsheet it is tied to. Below is the code:
Report Script
The script is activated by a custom menu bar. An example of the menu bar is here below:
Menu Bar Code
Upon clicking into the custom menu bar, I would like the newly generated "report" spreadsheet to open. Since this script creates a new spreadsheet each time it is ran, the URL will be different each time, therefore, I am not sure how to incorporate the variable URL into an HTML file. Below are some lines of code I have used to open files upon using the custom menu bar:
HTML File Code
In the above image, "+ ssNewURL +" is where the known URL to the desired destination would go. I attempted to reference a variable I used earlier (ssNewURL) which gets the URL of the newly generated sheet, but it did not work. I have tried to get around the formatting issue of the HTML file which requires the URL to be a string; I've tried changing locations of double "" quotations,
and single '' quotations. Whatever I have tried, the HTML file refuses to open.
I am extremely new to coding, I understand the logic behind it, however, I am very unfamiliar with every function/formatting of Google Scripts coding.
Any suggestions or workarounds for getting the HTML part of the code to open the dynamically changing URL would be greatly appreciated!
When you call window.open(), the URL also needs to be enclosed in quotation marks.
let htmlOutput = HtmlService.createHtmlOutput(
"<script type='text/javascript'>" +
"window.open('" + url + "', '_blank');" +
"google.script.host.close();" +
"</script> "
);
Another option would be to pass the URL of your spreadsheet to the HtmlTemplate object as a property:
let template = HtmlService.createTemplateFromFile("popup");
template.url = url;
return SpreadsheetApp.getUi().showDialog(template.evaluate());
Calling evaluate() on an HtmlTemplate object will execute the embedded JS code and place all variables you passed to the template in scope.
popup.html
<body>
Opening your spreadsheet...
<input type='hidden' id="hidden-field" value='<?!= url ?>' />
<script>
var url = document.getElementById("hidden-field").value;
window.open(url, "_blank");
google.script.host.close();
</script>
</body>

HTML Form to Remove ?get=info on POST Submit?

I have several pages that are arrived on with valid GET data, such as http://website.com/?id=12345
I have a generic HTML form that is pulled onto many different pages using php's "require" and submits using POST. Regardless of which page this form is located on, it should always submit back to that same page. However, after the form is submitted, I would like the ?id=12345 to be stripped out.
So, for example, if the user is on http://website.com/new.php?id=12345, it should post back to http://website.com/new.php. If the user is on http://website.com/old.php?id=12345, that same form it should post back to old.php
Previously the best solution I found was to style the form as such:
<form action="?" method="POST">
Which will change all links to http://website.com/new.php? or http://website.com/old.php? which is very close, but not perfect.
As it turns out, I finally found the solution to my problem by using JavaScript:
url = location.href;
qindex = url.indexOf("?");
This can pull whatever is on the address bar as a string and find the index of the first ? mark. From there:
if(qindex != -1)
tells me that there is a ? mark
var plainUrl = url.substring(0, qindex);
Can get, as a string, everything up to the ? mark, but not after. Finally:
window.location.replace(plainUrl);
Will rewrite the address bar to the plain URL, not including the ? or whatever comes after, and without redirecting the browser.
Since your page will not undergo any server-side processing, you can achieve what you want via a combination of the following two tricks.
First, change your particular querystring to a hash, which is thereafter directly editable without triggering a page reload:
http://yourdomain.com/page.html#search=value
Then modify such a script as this to do what you want to do, according to the query string passed in.
<script type='text/javascript'>
// grab the raw "querystring"
var query = document.location.hash.substring(1);
// immediately change the hash
document.location.hash = '';
// parse it in some reasonable manner ...
var params = {};
var parts = query.split(/&/);
for (var i in parts) {
var t = part[i].split(/=/);
params[decodeURIComponent(t[0])] = decodeURIComponent(t[1]);
}
// and do whatever you need to with the parsed params
doSearch(params.search);
</script>
now you can delete the query string suffix in the following way:
As detailed elsewhere, namely hide variables passed in URL, it's possible to use JavaScript's History API in modern browsers.
history.replaceState({}, null, "/index.html");
That will cause your URL to appear as /index.html without reloading the page
This little gem is explained in more detail here:
https://developer.mozilla.org/en-US/docs/Web/API/History_API

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.

Using HTML file to output a PDF

So I've got a HTML file, that I am using to send emails, but in some instances I want it simply to use that file to create a PDF of the same template.
I've got it functioning for the most part - it creates the file, runs the evaluations and gets the content, but it doesn't actually render the html. It simply leaves all the html notation in place.
For example, it outputs a pdf but it reads:
Dear Martin, <br />
instead of:
Dear Martin
How do I make sure it renders the HTML so that the PDF is laid out correctly, and doesn't have the html code noted in the text?
Here's the code:
var docName = "test";
var htmlBody = HtmlService.createHtmlOutput(template.evaluate().getContent()).getContent()
var doc = DocumentApp.create(docName);
doc.appendParagraph(htmlBody);
doc.saveAndClose();
DocsList.createFile(doc.getAs('application/pdf')).rename(docName);
You can use your existing HTML as a blob, and convert it to PDF like this:
var htmlBody = HtmlService.createHtmlOutputFromFile('my_file_within_script_project.html').getContent();
var blob = Utilities.newBlob(htmlBody, 'text/html').getAs('application/pdf').setName('my_output_in_drive.pdf');
DriveApp.createFile(blob);
The best option when you want to create a PDF from a template is to use Google Docs directly so that no formatting is lost and also avoid the problem you are facing.
Why don't you just create your template directly in Google Docs. Have some placeholders such as {name} instead of the actual name. Instead of using template.evaluate(), you can do a find and replace in the doc.

JQUERY: Dynamically loading and updating iframe content on change() / keyup()

I've been working on a custom CMS in drupal for about two or three weeks now, and I keep running into this same problem. I'm trying to load a dynamically generated url (by extracting the node id of the target drupal page into $resultCut and appending it to the baseurl of the website). This iframe is embedded next to an instance of CKEditor, and the idea is to have the content in the iframe change when the fields in CKEditor are modified. I have the following Jquery:
<script type="text/javascript">
$(document).ready(function(){
baseurl = urlhere;
url = baseurl+"<?php echo $resultCut ?>"
$('#EmuFrame').attr('src', url);
var HTML = $('#EmuFrame').contents().find('body').html();
alert ( "LOADING COMPLETE" + HTML );
});
$('#edit-field-mobile-page-header-label-0-value').change(function () { // writes changes to the header text to emulaor
var curr = $(this).val();
$('#EmuFrame').contents().find("h1").text(curr);
});
$('#edit-body').keyup(function(e) { // writes changes to the body text to emulator
var curr = $(this).val();
currhead = $('#EmuFrame').contents().find("h1").html();
$('#EmuFrame').contents().find('#content').html("<h1>"+currhead+"</h1>"+curr);
});
where #EmuFrame is the id of an iframe, and the #edit-* tags are the ids of fields in CKEditor that I am monitoring for change. When the user types, the keyup() or change() events is supposed to grab the new html and swap it with the html in the iframe.
As of right now, the LOADING COMPLETE alert fires, but there is no html in the alert. I noticed that the content of the iframe loads AFTER the alert fires, however, which is what led me to believe that it's a problem with the order in which the events trigger.
Further, I had an alert in the callback function of keyup that returned the new html [ alert(curr) ] that was generated when a user started typing, and this alert returns html (although, it is being grabbed from CKEditor). However, the iframe does not reflect any changes. If I append [ alert (currhead) ] though, nothing is alerted at all.
It might be of interest to note that the source url is technically on a different domain than the parent. however, I used a workaround (i'm pretty sure it works, because I've previously gotten the whole html replacement thing working, and then somehow it broke). Also, neither Firebug nor Chrome's console report any XMLHttpRequest errors. Also, I keep getting this error: "Uncaught Syntax error, unrecognized expression: [#disabled]" and I'm not sure what it means, and whether its relevant to my problem as stated above.
That was a ridiculously long plea for help, so THANKS FOR READING, and thank you for any help!!
Your note about about the cross-domain iframe src is worrisome -- you shouldn't be able to access its contents with javascript. Nevertheless:
You have these two lines in quick succession:
$('#EmuFrame').attr('src', url);
var HTML = $('#EmuFrame').contents().find('body').html();
Try waiting for the iframe to load first:
$('#EmuFrame').load(function() {
var HTML = $('#EmuFrame').contents().find('body').html();
}