r/GoogleAppsScript • • 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:

  1. 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?

  2. 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?

  3. Also, is there a way to make the first half (left half) tolerate symbols, like parentheses, slashes, and quotation marks?

  4. 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?

  5. Is there a way to remove all the bookmarks from a document? UPDATE: solved this myself!

  6. 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")
}
8 Upvotes

8 comments sorted by

1

u/kwijibob May 10 '20

I don't know if you can get the menu in every doc, but you could set a trigger to run your code on every doc in a particular directory.

I'll need to think further about your other questions.

How do you put the files into gdrive in the first place?

2

u/thefourthchipmunk May 10 '20 edited May 10 '20

Thanks. I actually was able to set up the directory trigger! I deleted it from this code to simplify things. Sometimes I need to use the doc-specific trigger.

Half the time I upload them as docx and convert them to doc, and this is where I have the problem with too many bookmarks.

The other half I copy-paste directly into doc.

2

u/thefourthchipmunk May 10 '20

Hey I did the bookmarks thing myself!!

var bookmarks = myFile.getBookmarks()
for (i=0; i<bookmarks.length; ++i)
{
bookmarks[i].remove()
}

2

u/lordph8 May 10 '20

More of a sheet junky, and I just moved and have no internet. But...

Shouldn't

myFile.replaceText() be myFile.getText().replaceText()?

See if you can't add the string .toLowerCase() to the end of getText? Usually JavaScript let's you define the case of imported string data. So getText().toLowerCase(). If you can do that you you can just replace text based on lower case. Hope that works.

-1

u/11111v11111 May 11 '20

Dude, don't be that guy. Pay a professional who does this for a living instead of ask for free advice.

1

u/thefourthchipmunk May 11 '20

How much are you asking for?

0

u/[deleted] May 10 '20

[deleted]

1

u/thefourthchipmunk May 10 '20

Thanks. Follow up question: can you give more specifics?

1

u/gh5000 May 10 '20

Is your account on a paid business subscription? Or are you on a personal Gmail account? Your answer changes the specifics and complexities