I am trying to make a table generated in an iFrame downloadable in a CSV. I have made a JSFiddle (https://jsfiddle.net/22u6o6wu/3/) that can load the iFrame from Fusion Tables, however, I cannot get it to work. Here is the code that I am using:
This part deals with loading the url in an iFrame
function myFunction(myInput) {
var userInput = document.getElementById("myInput").value;
var Input = document.getElementById("demo").innerHTML = userInput;
if (userInput){
var startUrl = "https://fusiontables.google.com/embedviz?viz=GVIZ&t=TABLE&q=select+col0%2C+col1%2C+col2%2C+col3+from+1XIDlbUpAJLcUtDf-cYoOZzpLi2KSN0ZH93sHJxVq+where+col3+%3D+'";
var endUrl = "'&containerId=googft-gviz-canvas";
var URL = startUrl+Input+endUrl;
document.getElementById("frame").innerHTML = '<iframe src="' + URL +
'" width="1000" height="720" scrolling="yes" border="0" sandbox="allow-same-origin allow-scripts" id="iframeId"></iframe>';
}
}
This is the CSV part
function download_csv(csv, filename) {
var csvFile;
var downloadLink;
// CSV FILE
csvFile = new Blob([csv], {type: "text/csv"});
// Download link
downloadLink = document.createElement("a");
// File name
downloadLink.download = filename;
// We have to create a link to the file
downloadLink.href = window.URL.createObjectURL(csvFile);
// Make sure that the link is not displayed
downloadLink.style.display = "none";
// Add the link to your DOM
document.body.appendChild(downloadLink);
// Lanzamos
downloadLink.click();
}
function export_table_to_csv(html, filename) {
var csv = [];
//var iframe = document.getElementById('googft-gviz-canvas').contentWindow.document.body.innerHTML;
//var iframeDoc;
// if (window.frames && window.frames.iframeId &&
// (iframeDoc = window.frames.iframeId.document)) {
// var iframeBody = iframeDoc.body;
// var ifromContent = iframeBody.innerHTML;
// }
var rows = document.querySelectorAll("table tr");
for (var i = 0; i < rows.length; i++) {
var row = [], cols = rows[i].querySelectorAll("td, th");
for (var j = 0; j < cols.length; j++)
row.push(cols[j].innerText);
csv.push(row.join(","));
}
// Download CSV
download_csv(csv.join("\n"), filename);
}
document.querySelector("button").addEventListener("click", function () {
var html = document.querySelector("table").outerHTML;
export_table_to_csv(html, "table.csv");
});
The CSV part was taken from: https://jsfiddle.net/gengns/j1jm2tjx/
So far I have tried the following methods:
document.getElementById('googft-gviz-canvas').contentWindow.document.body.innerHTML
And the top answer from: How to get the body's content of an iframe in Javascript?
However, I have been unable to implement these solutions.
In the inspector, I have been able to find the HTML for the table that is loaded, and the associated id's, I then tried to use these id's with the above methods, however, that still did not work.
I would preferably like to stick with Javascript, as I do not have any experience with jQuery, and hence would not be able to modify, explain or fix the code.
Thanks.
Related
I want to search for a string and then replace it with an image on google docs. For that, I want to get the index of the string and then replace it with an image. But I'm unable to get the index of the string so far.
Below is a snippet of what I am doing:
var element = '<<19>>';
options = {muteHttpExceptions: true};
var resp = UrlFetchApp.fetch(mylist[x-1], options);
var image = resp.getBlob();
//getting the index of element and then replacing it with image
var rangeElement = body.findText(element);
var foundElement = rangeElement.getStartOffset();
body.replaceText(element, body.insertImage(foundElement, image));
I've tried using findText(searchPattern) but it didn't work as it returns a range element and I'm getting output as 0 everytime.
This is how you find text on your document and replace it with an image:
function findAndReplaceWithImage() {
var element = "<<19>>";
var doc = DocumentApp.getActiveDocument().getBody();
var image = "your image url";
var blob = UrlFetchApp.fetch(image).getBlob();
var paragraphs = doc.getParagraphs();
for (var i = 0; i < paragraphs.length; i++) {
var text = paragraphs[i].getText();
if (text === element) {
//Found your match
doc.removeChild(paragraphs[i]);
doc.insertImage(i, blob);
}
}
}
Hope this helps!
I'm trying to replace all img src's on my page, when they have a certain url in them. So far I'm able to log all the replaced img src's, but my final step would be to change them on my page.
function replaceMyImgs() {
// put live url here
var liveUrl = "via.placeholder.com";
// find the local url
var localUrl = "testdomain.com";
// replace the local url for the live url
var newUrl = localUrl.replace(/testdomain.com/g, liveUrl);
// console.log(newUrl);
// get all images and push them in an empty array
var imgs = document.getElementsByTagName("img");
var imgSrcs = [];
for (var i = 0; i < imgs.length; i++) {
imgSrcs.push(imgs[i].src);
}
imgSrcs.forEach(function(src) {
// log all the found img srcs
var newSrc = src.replace(/testdomain.com/g, liveUrl);
imgs.src = newSrc;
console.log(imgs.src);
});
}
window.onload = replaceMyImgs;
See my pen: https://codepen.io/kleefaan/pen/yqzBVv
Instead of pushing all elements into an array you can do the replace in the for loop like this:
// get all images
var imgs = document.getElementsByTagName("img");
for (var i = 0; i < imgs.length; i++) {
var newSrc = imgs[i].src.replace(/testdomain.com/g, liveUrl);
imgs[i].src = newSrc;
console.log(imgs[i].src);
}
Am new to exporting html table to csv. The code below could export only the current visible page, but I want to be able to download from pages 1 -- 200 for instance at a click of a button.
// download html table to csv file
function downloadCSV(csv, filename) {
var csvFile;
var downloadLink;
csvFile = new Blob([csv], {
type: "text/csv"
});
downloadLink = document.createElement("a");
downloadLink.download = filename;
downloadLink.href = window.URL.createObjectURL(csvFile);
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
downloadLink.click();
}
function exportTableToCSV(filename) {
var csv = [];
var rows = document.querySelectorAll("table tr");
for (var i = 0; i < rows.length; i++) {
var row = [],
cols = rows[i].querySelectorAll("td, th");
for (var j = 0; j < cols.length; j++)
row.push(cols[j].innerText);
csv.push(row.join(","));
}
//download csv file
downloadCSV(csv.join("\n"), filename);
}
After uploading images, what I want is I want to display that image. It is working properly when the image is of .jpg extension. But when I upload pdf files its gets uploaded but it is not displaying the file.
JQUERY
function setImagesSitePlot(JsonObject) {
for (i = 0; i < JsonObject.length; i++) {
var obj = JsonObject[i].Filename;
var obj2 = "ImgSitePlotManual";
var obj3 = JsonObject[i].FileType;
var dataImageName = JsonObject[i].ImageName;
var ImgObj = parent.document.getElementById(obj2);
ImgObj.src = SharedFilePath + obj;
$(ImgObj).attr("data-imagename", dataImageName);
}
}
HTML
<img width="500" class="img-responsive" id="ImgSitePlotManual" data-imagename="" style="width: 606px;" />
You either have to use an image representing the pdf file icon that you return in your JSON string once the uploaded file is pdf.
Or you can display the pdf file in an object element:
<object data='http://website.com/nameoffolder/documentname.pdf#toolbar=1'
type='application/pdf'
width='100%'
height='700px' id='PdfSitePlotManual'>
You cannot view pdf in an image.
Make use of the fileType property you are already returning:
for (i = 0; i < JsonObject.length; i++) {
var obj = JsonObject[i].Filename;
var obj3 = JsonObject[i].FileType;
var dataImageName = JsonObject[i].ImageName;
if(obj3 == 'img')
{
var ImgObj = parent.document.getElementById("ImgSitePlotManual");
ImgObj.src = SharedFilePath + obj;
$(ImgObj).attr("data-imagename", dataImageName);
}
else if(obj3 == 'pdf')
{
var PdfObj = parent.document.getElementById("PdfSitePlotManual");
PdfObj.setAttribute('data', SharedFilePath + obj);
}
}
I'm trying to save all the link strings into a text document, but it only saves the last link in the document (in this case Youtube.com).
I want it to save all the links to the saved txt document, what am I doing wrong?
https://jsfiddle.net/zfL2hzvp/4/
var links = document.querySelectorAll('a');
// Loop through all links
for (var i = 0; i < links.length; i++) {
// Store links in variable
var linksArray = links[i];
// Works fine in console
console.log(linksArray);
}
// Create text document — only saves 1st link in text doc
var textDoc = document.createElement('a');
textDoc.href = 'data:attachment/text,' + encodeURI(linksArray);
textDoc.target = '_blank';
textDoc.download = 'myFile.txt';
textDoc.click();
Can someone help me out here?
Thank you! :-)
(function() {
var links = document.querySelectorAll('a');
var linksArray = [];
// Loop through all links
for (var i = 0; i < links.length; i++) {
// Store links in variable
linksArray.push(links[i]);
// Works fine in console
console.log(linksArray);
}
// Create text document — only saves 1st link in text doc
var textDoc = document.createElement('a');
textDoc.href = 'data:attachment/text,' + encodeURI(linksArray.join('\n'));
textDoc.target = '_blank';
textDoc.download = 'myFile.txt';
textDoc.click();
})();
https://jsfiddle.net/um4qhsks/1/