Matt Cashatt
Matt Cashatt

Reputation: 24218

How do I use the .NET Google Docs API SDK to find and replace text in a given file?

Looking for a way to find and replace tokens in a Google Doc using the Docs API via the C#/.NET SDK.

Upvotes: 1

Views: 302

Answers (1)

Matt Cashatt
Matt Cashatt

Reputation: 24218

Figured it out thanks to this example on SO. Here is a code sample in case it helps others:

private void mergeText()
        {
            var documentId = "{your Google doc ID}";
            var requests = new List<Google.Apis.Docs.v1.Data.Request>();
            var repl = new Google.Apis.Docs.v1.Data.Request();
            var substrMatchCriteria = new SubstringMatchCriteria();
            var replaceAlltext = new ReplaceAllTextRequest();

            substrMatchCriteria.Text = "{{foo}}";
            replaceAlltext.ReplaceText = "fizz buzz";            

            replaceAlltext.ContainsText = substrMatchCriteria;
            repl.ReplaceAllText = replaceAlltext;

            requests.Add(repl);

            BatchUpdateDocumentRequest body = new BatchUpdateDocumentRequest { Requests = requests };

            Program.docService.Documents.BatchUpdate(body, documentId).Execute();
        }

Upvotes: 1

Related Questions