I’m having issues with the Google Docs API in my Apps Script project. I’m trying to make copies of a Google Doc and then replace some text in each copy. I’m using the batchRequests library by tanaike for this.
The file copying part works fine, but I’m getting a 404 error when I try to replace text using batchUpdate and replaceAllText.
I’ve dealt with similar API issues before, and it can be quite frustrating. One thing that’s not immediately apparent from your code is whether you’re using the correct authentication method. For Google Docs API calls in Apps Script, you typically need to use OAuth2 with the appropriate scopes.
Try adding this to your script:
DriveApp.getFiles(); // This forces OAuth prompt
Run it once to authorize the necessary scopes. Also, double-check that you’ve enabled the Docs API in the Google Cloud Console for your project.
If that doesn’t work, you might want to consider using the built-in DocumentApp as others have suggested. It’s generally more straightforward for simple text replacements within Apps Script.
Lastly, ensure your document IDs are correct and that you have edit access to all the docs you’re trying to modify. Good luck!
I’ve encountered similar issues when working with the Google Docs API, and it can be quite frustrating. From my experience, the 404 error you’re seeing often indicates that the script doesn’t have the necessary permissions to access or modify the documents.
Here are a few things you might want to check:
Make sure you’ve enabled the Google Docs API in your Apps Script project. Go to Resources > Advanced Google Services and turn on the Google Docs API.
Verify that your script has the required scopes. Add this to the top of your script:
// @OnlyCurrentDoc
This limits the script’s access to the current document and may resolve permission issues.
Double-check that the document IDs you’re using are correct and that your account has access to these documents.
Instead of using the REST API directly, try using the built-in DocumentApp service in Apps Script. It’s often more reliable for simple text replacements.
If none of these solve the problem, you might want to try using the official Google APIs Client Library for JavaScript instead of the batchRequests library. In my experience, it’s more stable and easier to debug.
Hope this helps! Let me know if you need any further clarification.