add custom menu to script-generated Google Document - google-apps-script

I know how to add a custom menu to a Google Document (when the script is 'inside' the Document -- see here), but if I am creating a Document with a script
var doc = DocumentApp.create('Doc name');
I can't figure out how to add a custom menu to that document at the same time. How do I get a reference to the Document?

What you can do is start from a 'template'. So have a template document which contains a script that adds the menu to the document. And in your script, make a copy of this template document and use it.
You can use the DocsList API to make copies of existing templates

Related

Put the URL (prepended with the text "Parent_") for a particular Google Doc into that Google Doc's own header and footer

Overview
Essentially I want to be able to migrate my Google Docs to a regular website while maintaining the links I had created between my Google Docs.
A proposed solution
What do you think about having a Google Apps Script put the URL (prepended with the text "Parent_") for a particular Google Doc into that Google Doc's own header and footer, once and only once, when that Google Doc is initially created? (I almost never use headers and footers therefore it seems like it might be reasonable to use them as if they were akin to fields in a database).
For example,
Create a Google Doc.
Let's say, that Google Doc, has the following URL https://docs.google.com/document/d/1XwCyQ2MAIJgcLwfmR9-_JQq97H9iVLbGXNbpbzq1-Oo/
Put Parent_https://docs.google.com/document/d/1XwCyQ2MAIJgcLwfmR9-_JQq97H9iVLbGXNbpbzq1-Oo/ into the header and footer.
That way when a Google Doc is exported as an HTML document, it will contain 2 instances of the following string: Parent_https://docs.google.com/document/d/1XwCyQ2MAIJgcLwfmR9-_JQq97H9iVLbGXNbpbzq1-Oo/ into the header and footer
Then, with a simple find/replace (RegEx) the HTML files could be relinked.
To be safe, I might run a Google Apps Script once a week to confirm that each Google Doc contained two instances of "Parent_" followed by the URL.
Finally, what if, say, once a week I were to export my Google Doc's as HTML files, upload them to a regular website, and run, say, a Python script to ensure that every Google Doc that contained "URL001" that was not prepended with "Parent_" actually linked to "Parent_URL001"
Fundamentally, it's a kludge. I know that. But I'm trying to circumvent, what is effectively, a "soft lock-in" feature in Google Docs for those who create hyperlinks between Google Docs.
My problem described in detail
Frequently I link one Google Doc to another Google Doc. As a result, I have created something that is similar to a wiki. For example let’s suppose I had created two Google Docs: Google Doc #1 and Google Doc #2.
Subsequently let’s suppose I had created a link (a hyperlink) in Google Doc #1 to Google Doc #2. Of course that's an extremely simple example. Let’s make it more complex. Imagine I had created a couple of thousand Google Docs with many links (hyperlinks) between them.
Of course backing up those Google Docs would be trivial either by using Google Takeout or rsync. However, what would happen if I wanted to move those Google Docs to a regular website? Then the myriad hyperlinks I had created would fail to point to the documents on my regular website.
That is, on my regular website, if I were to click on the link on the page which contained the contents which had been contained in Google Doc #1 (https://my_regular_website.com/google_doc_001) then instead of opening a link on my regular website to the page which contained the contents which had been contained in Google Doc #2 (https://my_regular_website.com/google_doc_002) , the link would point to the original Google Doc #2 (https://drive.google.com/drive/folders/google_**doc_002**)
Why not use Notion or at least a wiki?
I like using Google Docs as a word processor. Sometimes I use Google Docs to write essays. Sometimes I use Google Docs to create documentation. Sometimes I use Google Docs to collaborate with others (instead of emailing). Furthermore, I often use Google Docs’ outline format, styles, and voice typing.
As far as I know, neither Notion nor any wiki have all of the features listed above.

Google Spreadsheet parsing

I am publishing Google Spreadsheet table via File -> Publish to the Web ... Example published table here.
From this shared link I am parsing out a blank HTML table structure and display it on our website. I am removing all inline styles applied by Google.
Anyone knows if it is possible to:
Apply custom CSS class to cells inside Spreedheets that would google apply to the published HTML table?
Is it possible to defina a row inside a spreadsheet that would google publish as a <thead><tr><th></th></tr></thead>. For example I would like the first row to be a table header instead of table body.
Or any workarounds how to effectively style this table.

How to generate an HTML view in App Script from a Google Sheet

I have a client that uses Google forms to take orders and wants to be able to turn the resulting Sheet into a document of Packing Slips. Being a python person I am currently taking her .tsv and making nice HTML printable docs but want to be able to make a plug-in for her so that on any Sheet of this type she can click "Make Packing Slips" and it either opens a new tab with generated HTML (I'm ok using googles templating) or maybe even convert the HTML into a PDF and save it to her drive? Either way I'd rather design the document all in HTML and CSS rather than trying to make a google doc if at all possible. Is the feasible? Any starting points? Thanks!
You need to learn about Google App Script => https://developers.google.com/apps-script/overview
Then these Links should suffice
HTML Service: Create and Serve HTML
Google Container-bound Scripts for Google App Script (Can not post link for this due to low reputation)

Updating html of a Google Site's page with multiple blocks using Google Apps Script

I've created a page using "Web Page" template on my Google Sites, and I want to update the page using Google Apps Script. My code looks like this:
var template = HtmlService.createTemplateFromFile('mytemplate'); // meaning 'mytemplate.html'
var content = template.evaluate().getContent();
var site = SitesApp.getSiteByUrl('https://sites.google.com/site/example/');
var page = site.getChildByName('home');
page.setHtmlContent(content);
This works pretty well if the page's layout is "One column (simple)". Now my question is, if I choose one of the other layouts, how can I update each individual blocks using setHtmlContent()? Actually, I tried using "Two column (simple)" layout which has 2 blocks. setHtmlContent() changed the left column's html code and cleared the other column.
EDIT: What I actually wanted to do is to create a page with a gadget and update the page using Google Apps Script. According to this issue tracker https://code.google.com/p/google-apps-script-issues/issues/detail?id=572, it looks like we may encounter some problems when updating such page with setHtmlContent(). So I came up with an idea to create a multiple-column page, place a gadget in a column, and only update the HTML code of the other column with the Script to avoid such issue.
You can also create a Stand Alone Web App with Apps Script, then put an Apps Script Gadget into a Sites page. You can have multiple Apps Script Gadgets. I don't know what is triggering the change of the HTML. For all practical purposes, a Stand Alone Apps Script HTML Service App, is basically the same thing as a web site. You don't have a nice, understandable URL, but that won't show up in the Apps Script Gadget. Google will display a message: "This content was not created by Google"
I don't think you'll be able to inject HTML into multiple columns. You could create HTML that has multiple columns in it, and then add that HTML to the one column. So, you would be creating your own columns in HTML.

Google Apps Script add code to copy of a document

I created a document bound script in Google Docs that allows me to select a part of some text in a document. Then when I click my menu button it will make a copied document with only the selected text in it. The script then shares the copied document and emails the person that it was shared with them.
I would like it to be possible that the user of the shared document is able to read that document, and send a message back via a form on what he thought about it. So he would also go to a customized menu button, click on it, a form would pop up and he would email me back with the text.
I know how to do this in Google Apps Script manually. I can go to the copied document and just add that script. But how do I automize this? I looked at the Google Apps Script references but could not find anything.
It is not possible to embed a script in a document using a script.
You could use a template that has a script , make a copy of it and add your content to that copy instead of creating the new doc from scratch.
Depending on the document content it might be very simple ...or just a bit more complex since you will have to add each element in turn. There are a few examples of this process here: How to copy a template and insert content from another document? :