I have a trigger set up in Google Sheets so a URL is automatically opened in a new browser window. This works if the URL is hardcoded. I want the URL to be a variable. How do I pass the URL variable from Apps Script to HTML script? I'm a novice coder so please explain like I'm 5.
This function works if the URL is text like 'https://www.google.com'
function openMap() {
var maplink = SpreadsheetApp.getActiveSheet().getRange("B2").getValues();
var js = "<script>window.open('https://www.google.com', '_blank', 'width=800, height=600');google.script.host.close();</script>";
var html = HtmlService.createHtmlOutput(js)
.setHeight(10)
.setWidth(100);
SpreadsheetApp.getUi().showModalDialog(html, 'Now loading.');
}
This function does not if the URL is a variable (I checked the maplink value and its a valid URL)
function openMap() {
var maplink = SpreadsheetApp.getActiveSheet().getRange("B2").getValues();
var js = "<script>window.open('maplink', '_blank', 'width=800, height=600');google.script.host.close();</script>";
var html = HtmlService.createHtmlOutput(js)
.setHeight(10)
.setWidth(100);
SpreadsheetApp.getUi().showModalDialog(html, 'Now loading.');
}
You can join strings in the following manner:
...
var url = 'https://www.google.com';
var js = "<script>window.open('" + url + "', '_blank', 'width=800, height=600');google.script.host.close();</script>";
...
Basically
you close the first part of your hardcoded string by closing the quotes
add the dynamical variabl with +
continue the hardcoded string by appending the second part with + and opening the quotes again
I hope this is clear!
Related
I´m calling a tcpdf php page from javascript, like this :
let wParams = "myParamsAreLarge";
let wUrl = "www.mydomain.com/tcpdf/page.php?" + wParams;
window.open(wUrl, "_blank", "width=500, height=400");
this work fine.
wParams are dinamically created so when there is a lot of data, i receive error :
Request-URI Too Long
The requested URL's length exceeds the capacity limit for this server.
How can i call my page.php to display tcpdf with post parameters to get the same behavour, and so avoid calling php page with get parameters ?
Regards, Juan
The solution is : change :
$pdf->Output('file.pdf', 'I');
to:
return $pdf->Output('file.pdf', 'S');
then in javascript:
let dataBase64 = resultData;
let ventanaPDF = window.open("", "_blank", "width=450, height=650, nodeIntegration=no, modal");
let contenidoFinalVentana = '<embed width=100% height=100% type="application/pdf" src="data:application/pdf;base64,' + escape(dataBase64) + '"></embed>';
ventanaPDF.document.write(contenidoFinalVentana);
I’m seeking how to output SharePoint Document Library Files to csv file. I found script that get me almost there, but I can’t figure out how to update the code to export the information to a csv file instead to the console.log() or to an alert(). Everything I tried breaks the code. I review other JavaScript concept that shows the how to add out to CSV but I again the script concept breaks the code I’m trying to modify. The script I am using. In addition, the script output the file names. I like to get help on how I can not only output the file name, but I like to output, modified date, created date, and the link to the file. I hope this is possible and I appreciate any help in achieving this concept. Script I'm using follows below.
jQuery(document).ready(function() {
var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";
$.getScript(scriptbase + "SP.Runtime.js", function() {
$.getScript(scriptbase + "SP.js", function() {
$.getScript(scriptbase + "SP.DocumentManagement.js", createDocumentSet);
});
});
});
var docSetFiles;
function createDocumentSet() {
//Get the client context,web and library object.
clientContext = new SP.ClientContext.get_current();
oWeb = clientContext.get_web();
var oList = oWeb.get_lists().getByTitle("Fact Sheets & Agreements");
clientContext.load(oList);
//Get the root folder of the library
oLibraryFolder = oList.get_rootFolder();
var documentSetFolder = "sites/nbib/ep/Fact%20Sheets/";
//Get the document set files using CAML query
var camlQuery = SP.CamlQuery.createAllItemsQuery();
camlQuery.set_folderServerRelativeUrl(documentSetFolder);
docSetFiles = oList.getItems(camlQuery);
//Load the client context and execute the batch
clientContext.load(docSetFiles, 'Include(File)');
clientContext.executeQueryAsync(QuerySuccess, QueryFailure);
}
function QuerySuccess() {
//Loop through the document set files and get the display name
var docSetFilesEnumerator = docSetFiles.getEnumerator();
while (docSetFilesEnumerator.moveNext()) {
var oDoc = docSetFilesEnumerator.get_current().get_file();
alert("Document Name : " + oDoc.get_name());
console.log("Document Name : " + oDoc.get_name());
}
}
function QueryFailure() {
console.log('Request failed - ' + args.get_message());
}
Sample test script in chrome.
function QuerySuccess() {
//Loop through the document set files and get the display name
var csv = 'Document Name\n';
var docSetFilesEnumerator = docSetFiles.getEnumerator();
while (docSetFilesEnumerator.moveNext()) {
var oDoc = docSetFilesEnumerator.get_current().get_file();
//alert("Document Name : " + oDoc.get_name());
//console.log("Document Name : " + oDoc.get_name());
csv += oDoc.get_name();//+',' if more cloumns
csv += "\n";
}
var hiddenElement = document.createElement('a');
hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(csv);
hiddenElement.target = '_blank';
hiddenElement.download = 'DocumentList.csv';
hiddenElement.click();
}
I'm trying to put together a picker in google sheets.
Once a file has been uploaded to google drive, I want the url to be posted in the current cell in the spreadsheet.
This is my pickerCallback located in an html file:
var message;
function pickerCallback(data) {
var action = data[google.picker.Response.ACTION];
if (action == google.picker.Action.PICKED) {
var doc = data[google.picker.Response.DOCUMENTS][0];
var id = 'https://drive.google.com/open?id=' + doc[google.picker.Document.ID];
google.script.run.accessSpreadsheet();
} message = id;
document.getElementById('result').innerHTML = message;
}
This returns the url in the picker dialog box.
My accessSpreadsheet function looks like this and is located in a google script file:
function accessSpreadsheet() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var currentCell = sheet.getCurrentCell();
currentCell.setValue(message);
return spreadsheet;
}
The function runs but it cannot define message. Is there a way to access the variable defined in the function in the html file from the google scripts function? Or is there another better way to do this?
Solution:
Pass the string id from client side to server.
Snippets:
google.script.run.accessSpreadsheet(id);
function accessSpreadsheet(id) {
currentCell.setValue(id);
Reference:
Client-Server communication
I am opening a new tab to display a PDF file. I have the PDF file data on a bytearray, base 64.
I am able to get the data, and display it doing this:
downloadFile(strData, name) {
var newdata = "data:" + "application/pdf" + ";base64," + (strData);
var newWindow = window.open(newdata, "_blank");
newWindow.onload = function() { document.title = "My title"; }
return true;
}
The problem i am having is that i am not able to set the title to the new tab opened.
I would like to set a title like "PDF File" or just the name of the document (i am getting the file data and the file name separately and passing it to my downloadFile function.
Is there any way to set the title to this tab? Thanks in advance!
Try this:
....
var newWindow = window.open(newdata, "_blank");
newWindow.document.title = "Some title";
....
EDIT:
Another way to do this might be to send an iframe to a new window instead of opening it directly with the base64 string.
So something like:
var newWindow = window.open();
newWindow.document.write('<iframe src="data:application/pdf;base64,' + (strData) + '" frameborder="0" allowfullscreen></iframe>');
newWindow.document.title = "Your Title Here";
This worked for me.
var newWindow = window.open(newdata, "_blank");
setTimeout(function(){ newWindow.document.title = 'my new title'; }, 1000);
I need to load a url in an iframe on page load (which is working), then grab the whole url of that loaded iframe, then once the url is saved as a variable i want to find any text between "target" and "/" (example url http://www.example.com/user/target09576/index.php i wish to find the 09576) then redirect the whole page using the found text (09576) as part of the redirect url.
I know, bad explanation and not a great solution, but any help would be appreciated.
$(document).ready(function() {
var iframe = $('#wmif');
var url = '<? echo "".$redirectlocation.""; ?>';
iframe.attr('src', url);
var searchurl = iframe.contentWindow.location;
var rep = /(.*target\s+)(.*)(\s+\/.*)/;
var target = searchurl.replace(rep, '$2');
window.location = 'http://example.com/go/index.php?target='+target;
});
well the iframe loads the new location ok, but it doesn't do any of the rest.
I would do it similar to the below, same result but with jQuery and an updated regex
$(document).ready(function () {
var iframe = $('#wmif');
var url = ''<? echo "".$redirectlocation.""; ?>';
iframe.attr('src', url);
//var searchurl = iframe.contentWindow.location; <- Invalid
console.log($('#wmif').contents().get(0).location);
var searchurl = $('#wmif').contents().get(0).location.href;
console.log(searchurl);
var rep = /.*\/target(\w+)\/.*/;
var target = searchurl.replace(rep, '$1');
console.log(target);
window.location = 'http://domain.com/go/index.php?target=' + target;
});
Yet another solution
var matches = /target\s?([^\/]+)/.exec('http://www.domain.com/user/target09576/index.php')
or
var matches = /(?:target\s?)([^\/]+)/.exec('http://www.domain.com/user/target09576/index.php')
console.log(matches[1]); // 09576
Modify regex to this /(?:target)([^\/]+)/ if you don't expect a space between target and the number.