Popup Box with Link to other google Document in Google Document - google-apps-script

I have a script that I run from the menu of a google document. It essentially functions like this one: https://webapps.stackexchange.com/questions/47173/how-to-automatically-open-a-created-text-document-after-it-being-created/47649#47649.
I want a function that is similar the one in the link above except that the popup with the link is shown in a google doc rather than a google spreadsheet. That part of my script looks like this (title and url are defined above that part):
var docnew = DocumentApp.getActiveDocument();
var app = UiApp.createApplication().setTitle("Open Doc")
.setHeight(50).setWidth(400);
var vPanel = app.createVerticalPanel().add(app.createAnchor(title, url));
app.add(vPanel);
docnew.show(app);
I then get the following error. "TypeError: Funktion show in Objekt Document nicht gefunden", which is German and means "TypeError: Function show not found in object Document".
Can somebody help me with this?
Thanks a lot in advance.
Best,
Phil

The show method works only in spreadsheets, in Google Documents (and also in the new version of spreadsheets) you can use this code :
SpreadsheetApp.getUi().showModalDialog(argument);
The documentation shows some examples.
Your code snippet would be like this :
function showUi(){
var docnew = DocumentApp.getActiveDocument();
var app = UiApp.createApplication()
.setHeight(50).setWidth(400);
var vPanel = app.createVerticalPanel().add(app.createAnchor(title, url));
app.add(vPanel);
DocumentApp.getUi().showModelessDialog(app,"Open Doc");
}

Related

Insert hyperlink with variable into google docs using google scripts

I have a google Form, and the responses populate a response google Sheet, which has a google Script function to generate a google Document using a template.
I am trying to get the address entered in the Form (stored in the response Sheet) to become a hyperlink in the generated Doc.
I have been using the body.replaceText() to replace all the fields I need in the Doc:
body.replaceText("{{Date}}", date);
and its working well, but the address field I would like to become a hyperlink.
I have been trying to do it this way:
body.replaceText("{{Location}}", =HYPERLINK("http://www.google.com/maps/place/'+location+'"));
But that does not become a usable hyperlink, resulting with this in the Doc (please note while it becomes a hyperlink on this page it does not become a hyperlink in Docs):
=HYPERLINK("http://www.google.com/maps/place/myplacenotyours")
I have also tried:
body.replaceText("{{Location}}", location = HYPERLINK("http://www.google.com/maps/place/"+location+));
But this throws up syntax errors.
I have this var:
var location = e.values[2];
So perhaps it better to use that to create another var as a hypertext?
I am now trying:
var loclink = 'Hyperlink("http://www.google.com/maps/place/'+location+'","'+location+'")';
but that doesnt do it either... I'm now starting to think that one can't insert a hyperlink using replace method?
Sorry for the noob question, but I can't figure this out. Can you help me find a solution and put a var into a hypertext link and put that into the Doc as a link?!
Cheers.
Something like this:
function insertLink() {
var pattern = '{{Location}}';
var url = 'https://stackoverflow.com/a/69143679/14265469';
var text = 'how to paste a link';
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var next = body.findText(pattern);
if (!next) return;
var start = next.getStartOffset();
body.replaceText(pattern, text);
body.editAsText().setLinkUrl(start, start+text.length-1, url);
}

Why Google Apps Script throwing an "internal error" for Sites Services?

Here is my script, which basically iterate through drive folder, and put the file's blob as a attachment to some page on google site. It was working fine till day before yesterday, suddenly stop working after that.
function myFunction() {
var testpage = SitesApp.getSiteByUrl(siteURL).getChildByName("test");
var photofolder = DriveApp.getFolderById(folder_ID);
var filesinpf = photofolder.getFiles();
while(filesinpf.hasNext()){
var file = filesinpf.next();
var fblob = file.getBlob();
testpage.addHostedAttachment(fblob); //This line generating an error
}
}
Please help!
I had the same problem yesterday.
I have been working with this for a week, and yesterday I got an internal error in the last line:
function myFunction() {
var myFolder = DriveApp.getFolderById
("0B-ZOMOQnNEDOU9sWEV5SzlXVTQ");
var myFile =
myFolder.getFilesByName("Data.txt").next();
var myBlob = myFile.getBlob();
var myPage =
SitesApp.getSiteByUrl("https://sites.google.com/site/
demo ").getChildByName
("home/demoFileCabinet");
myPage.addHostedAttachment(myBlob);
}
Maybe a problem in Google Sites??. The code is correct.
I created an Issue Tracker too.
You can reproduce this error by using the sample code provided by google:
https://developers.google.com/apps-script/reference/sites/page#addHostedAttachment(BlobSource)
I created an enterprise support Ticket.
Keep you updated: [Case #14128120] Google Sites addHostedAttachment() not working
I got the following response from google:
Let me confirm that this is an issue on our end, filed with issue ID #68842220.
Please notice that our Engineering Team has already found the root cause for this.
At the moment, still, I can't confirm you when it will be fixed, but let me provide you with a simple workaround that will work while we wait for the fix: swap the "domain.com" and the "macros" parts of the URL when entering the URL in the dialog.
Sample, for url:
https://script.google.com/a/domain.com/macros/s/AKfycbwJfGpXIiWHfsCi-j66RuPMNx6kTFsdjYIbNOyufZptGA1tirm6/exec
try
https://script.google.com/a/macros/domain.com/s/AKfycbwJfGpXIiWHfsCi-j66RuPMNx6kTFsdjYIbNOyufZptGA1tirm6/exec

Google App Script Form not working

I am using google app script to create a form for uploading file. This is my Code.gs file:
var SPREADSHEET_FILE_ID = '1oQn6OLMzys8tVk1FLriOAmpzFJNazLRP-SwM7--eA58';
var folderId = "0B9TN_-yt-h0WZ0dnWndGWkw3UkE";
function doGet() {
var template = HtmlService.createTemplateFromFile('index');
// Build and return HTML in IFRAME sandbox mode.
return template.evaluate()
.setTitle('Web App Window Title')
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
};
And I am using all other codes almost similar to the url https://script.google.com/d/125dG42eB9lM4SPq64p0dpR2CBH4ohfHiqu9TvFNM8s4Ra7pt-7kHXoTM/edit?usp=sharing.
I am getting the following error.
3402363213-mae_html_user_bin_i18n_mae_html_user.js:42 Uncaught ReferenceError: "doc" is not defined.
Can anyone please help why this error is coming and how to prevent this. No for is not being submitted. It is hanged after I click the button of submission.
Since the problem started when you added these values, I think you need to recheck them.
var SPREADSHEET_FILE_ID = '1oQn6OLMzys8tVk1FLriOAmpzFJNazLRP-SwM7--eA58';
var folderId = "0B9TN_-yt-h0WZ0dnWndGWkw3UkE";
var SPREADSHEET_FILE_ID is expecting an spreadsheet ID like "1386834576" where as you provided "1oQn6OLMzys8tVk1FLriOAmpzFJNazLRP-SwM7--eA58", which is wrong. I think you got that from something like "https://docs.google.com/forms/d/1BqxyEG8RhtlM3MNuSbln6C1L1GLl3axdiSEijcwB5gY/edit" that's why it's asking you "doc" is not defined.

modal dialog with new spreadsheet api issue

I'm was trying to show html body of gmail message in modal dialog window but faced an error calling SpreadsheetApp.getUi() method.
...
var html = HtmlService.createHtmlOutput(threads[i].getMessages()[0].getBody());
SpreadsheetApp.getUi().showModalDialog(html, 'My add-on');
...
How I can call that dialog? Or may be there is better aproach to display message(some panel or sidebar)?
This prompt box is misleading, as the error must be elsewhere. The new version of Sheets not only does support the getUI method, I believe it's specifically designed for for the new version of sheets.
I've expanded on your previous question and tested this myself with the following code in the new version of sheets:
function getMail() {
var threads = GmailApp.getInboxThreads();
var messages = threads[0].getMessages()[0];
var raw = messages.getPlainBody();
return raw;
}
function dialogueBox(){
var raw = getMail();
var htmlOutput = HtmlService.createHtmlOutput(raw);
SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'For mk_yo');
}
and it displays the prompt with no issues, as shown here. Try creating a new sheet with a new script and run the code above. Additionally, you can try ensuring that the sheet that you're adding this to is definitely using the new version of sheets.
In relation to if there's a better approach by displaying a side panel, yes you can use custom sidebars in the new version of Google sheets(and only the new version), but as this still uses the 'getUI' method, this won't resolve your current error, as this is not where the issue lies.

Embed a list of forms in Google Site per DocList

I used the code given in
Embedding Google DocList in a Google Site
to embed a list of files (taken from a folder in Google drive) in a page of Google Sites. I like to know, if there's a way to modify the links on the output-page, so that they direct to the live-form of the spreadsheet and not to the spreadsheet-mode as it's set in the script.
EDIT : sorry about that, forget this first answer as there is actually a method to get form urls !!
here is how it works
function myFunction() {
var ss=SpreadsheetApp.getActive()
var formurl = ss.getFormUrl()
Browser.msgBox(formurl)
}
so it would be quite easy to embed a list in a site page,
here is a small code that does the job, online version here (needs authorization) be patient : can be slow to show up...
function doGet() {
var app=UiApp.createApplication().setTitle('Available Forms').setStyleAttribute('padding', '25');
var user = Session.getEffectiveUser().getUserLoginId();
app.add(app.createLabel('Available forms in Drive for '+user).setStyleAttribute('background', '#dddd33').setPixelSize(500, 18).setHorizontalAlignment(UiApp.HorizontalAlignment.CENTER).setStyleAttribute('fontWeight', 'bold'))
var flex=app.createFlexTable().setId('flex').setWidth('500').setBorderWidth(1).setCellPadding(2).setStyleAttribute('borderColor', '#dddd33');
var index = 0
var docs = DocsList.getAllFiles()
for (var n in docs){
if(docs[n].getFileType()=="spreadsheet"){
var ss=SpreadsheetApp.openById(docs[n].getId())
if(ss.getFormUrl()){
flex.setText(index, 0, docs[n].getName())
flex.setWidget(index, 1, app.createAnchor('open form', ss.getFormUrl()))
++ index
}
}
}
app.add(flex)
return app
}
again sorry that I forgot this feature.
-Forms are embedded in spreadsheet and don't appear as individual entities in your drive. Their ID (or URL) are not available from apps-script.... I'm afraid you'll have to encode the URLs manually.-
You could create a Document and name it the title of your Google Form. Stick the URL of the form inside the doc.