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.
Would I append something like the following to the script below?
DocumentApp.getActiveDocument().getFooter().getUrl()appendParagraph(URL);
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.
How the script below works
- Let say I were typing in Google Doc #1, and then
- I were to decide that I would like to create a link in Google Doc #1 to a new Google Doc (which does not yet exist).
- Furthermore, let's say I wanted this new Google Doc (which does not yet exist) to be named "Cherry pie."
- Therefore, while I was in Google Doc #1, I would type "Cherry pie", and then
- I would run the script below.
- As a result, in Google Doc #1 the line where the cursor is currently located (which consists of "Cherry pie") would now be hyperlinked to the Google Doc (which the script just created) named "Cherry pie."
function onOpen(e) {
DocumentApp.getUi().createMenu('NewDoc')
.addItem('Create', 'newDocWithSentense')
.addToUi();
}
function newDocWithSentense() {
const doc = DocumentApp.getActiveDocument();
const cursor = doc.getCursor();
const surroundings = cursor.getSurroundingText().getText();
const currentFileFolder = DriveApp.getFileById(doc.getId()).getParents().next()
const newDoc = DocumentApp.create(surroundings).getId()
DriveApp.getFileById(newDoc).moveTo(currentFileFolder)
cursor.getSurroundingText().setLinkUrl(DriveApp.getFileById(newDoc).getUrl())
}
A definition: the unique portion of the URL of the Google Doc
For example, “1cn2OX4U67mY925GzG80hRBYjpqq2conSi9xgYikgwIM” is the unique portion of a Google Docs with the URL https://docs.google.com/document/d/1cn2OX4U67mY925GzG80hRBYjpqq2conSi9xgYikgwIM
What I would like added to the script above
In addition to what the script above currently does, I would like the script to put the unique portion of the URL of the Google Doc named "Cherry pie" into both its header and footer. Why both its header and footer? For redundancy. That is, in case, for example, the header had been inadvertently edited I would hope the footer would still be unaltered (pristine).
For example, if the URL of the Google Doc named "Cherry pie" were https://docs.google.com/document/d/1cn2OX4U67mY925GzG80hRBYjpqq2conSi9xgYikgwIM, then the script would put “1cn2OX4U67mY925GzG80hRBYjpqq2conSi9xgYikgwIM” into the header and the footer of the Google Doc named "Cherry pie"
Why would I want the script to do such a strange thing?
First of all, I don't normally use headers and footers in Google Docs. Therefore, I see them as a convenient place to store the unique portion of the URL of the Google Doc (such as the Google Doc named "Cherry pie").
Second, let's imagine that at some point, I wanted to migrate my Google Docs to a regular website while maintaining the links I had created between my Google Docs. In such a case I could perform a find/replace (which is a simple regex) to recreate the links between my various documents (which I had downloaded from Google Drive) which had been Google Docs.
Why am I proposing such a kludgy solution? Why don't I propose a simpler, cleaner solution?
I read that, “You can use the 'contentRestrictions.readOnly' field on a `file' resource to lock a file and prevent modifications to the title, uploading a new revision, and addition of comments.” Source: Protect file content from modification
However, apparently Google Apps Script does not support the ability to prevent modifications to the title of a file while allowing the contents of the file to be edited. In other words, a Google Doc must be 100% locked or 100% unlocked.
Therefore, by having “1cn2OX4U67mY925GzG80hRBYjpqq2conSi9xgYikgwIM” which is the unique portion of a Google Docs URL, in the header and footer it would be trivial to recreate the links (which had existed between the Google Docs) on my regular website, by using a find/replace (which is a simple regex).
In other words, it would be trivial to “relink” documents that had pointed to https://docs.google.com/document/d/1cn2OX4U67mY925GzG80hRBYjpqq2conSi9xgYikgwIM
CodePudding user response:
Put Id into header and footer
function putIdIntoHeader() {
const doc = DocumentApp.getActiveDocument()
doc.getHeader().appendParagraph(`ID:${doc.getId()}`);
doc.getFooter().appendParagraph(`ID:${doc.getId()}`);
}
CodePudding user response:
When a new Document is created, as the default, there are no header and no footer. So, first, it is required to create the header and footer. When this is reflected in your script, it becomes as follows.
Modified script:
function newDocWithSentense() {
const doc = DocumentApp.getActiveDocument();
const cursor = doc.getCursor();
const surroundings = cursor.getSurroundingText().getText();
const currentFileFolder = DriveApp.getFileById(doc.getId()).getParents().next();
// I modified below script.
const newDoc = DocumentApp.create(surroundings);
const id = newDoc.getId();
const file = DriveApp.getFileById(id);
file.moveTo(currentFileFolder);
cursor.getSurroundingText().setLinkUrl(file.getUrl());
newDoc.addHeader().appendParagraph(id);
newDoc.addFooter().appendParagraph(id);
}
- In this modification,
id
is declared asconst id = newDoc.getId()
. And,file
is declared asconst file = DriveApp.getFileById(id)
. And,id
andfile
are used in the script. And then, the header and footer are added to the new Document. And, put the file ID to the header and footer.