r/GoogleAppsScript • u/thefourthchipmunk • May 10 '20
Question Hello. I wrote a script five years ago, it's essential to my business (teaching & tutoring), I use it several times a week, and it has always sucked because I don't program. Can you help me with some things?
I use it in Google Docs. For reference, an abridged version is at the bottom of this post. The real version is 1,000 lines long; the deleted part consists entirely of different permutations of the "replaceText" function.
Here are my questions/requests:
Currently, to run this on a document, I need to paste the document ID into fixFile("") and then I run the function from within the script. Is it possible to have this script appear in the add-ons menu for ALL my docs so I can just run it from within the doc?
Since replaceText is case-sensitive, currently I must use replaceText twice for each word: I write out the same string (i.e. the first string, left half of replaceText) in lowercase and uppercase. Is there a way to make the input for replaceText not be case sensitive?
Also, is there a way to make the first half (left half) tolerate symbols, like parentheses, slashes, and quotation marks?
When I use the 1,000-line version, sometimes the script times out!! (My documents are very long; they approach the 1.5M character limit.) Is there a better way to write the script to avoid that?
Is there a way to remove all the bookmarks from a document?UPDATE: solved this myself!Can I run replaceText with wildcards, so for example: A*C gets replaced with ZYX, regardless what * is?
Thank you.
function runMe()
{
fixFile("")
}
function fixFile(myFileID)
{
var myFile = DocumentApp.openById(myFileID)
var paras = myFile.getParagraphs()
var i
var style = {}
style[DocumentApp.Attribute.FONT_FAMILY] = 'Times New Roman'
for (i=0; i<paras.length; ++i)
{
paras[i].setSpacingAfter(0)
paras[i].setSpacingBefore(0)
paras[i].setAttributes(style)
}
myFile.replaceText("limited liability company", "LLC")
myFile.replaceText("Limited Liability Company", "LLC")
myFile.replaceText("government", "govt")
myFile.replaceText("Government", "govt")
}