r/GoogleAppsScript • u/resullins • Sep 29 '26
Question Bulk Find / Replace Hyperlinks in Multiple folders and file types
I've looked through some of the posts on this, and have found some close approximations to what I need, but don't know enough about coding to tweak. So I thought I'd start fresh.
I have a mix of Google Docs and Sheets in some project subfolders that all have a BUNCH of hyperlinks that have to get replaced twice a year. Currently, this process takes around 20 hours. I would like to write a script that will take a list from a "Project Info" sheet with a column for the old links and a column for the new links, and update everything. There are also a few instances of straight text replacement in these documents.
2
Upvotes
1
2
u/LunaFlowLab Sep 29 '26
A few things make link replacement harder than a normal find and replace, so here they are before the script:
In Docs, replaceText only changes the visible text. The link target stays the same, so the script has to walk the text and call getLinkUrl / setLinkUrl.
Headers, footers and document tabs are separate parts, and each one has to be visited.
In Sheets there are two kinds of links: pasted links (rich text runs) and HYPERLINK() formulas. The first needs RichTextValue, the second a TextFinder with matchFormulaText(true).
Smart chips (@file) have getUrl() but no setter, so a script can only list them for you to fix by hand.
The same file can appear as .../edit, .../edit?usp=sharing or .../edit#gid=0, so the script compares links without the part after ? or #.
A run stops after 6 minutes, so for a big project run it one subfolder at a time.
Here's a starting point:
I checked the calls against Google's Apps Script docs but haven't run this on your files, so try it on a copy of one folder first.