Background
I’m currently working on a task where I need to change text in a Google Docs template using information from an outside source. Since I am scripting in bash, I would like to utilize cURL to send the information and initiate the replacement process.
Current Setup
I have created a simple Google Apps Script function that correctly replaces placeholders within a document:
function updateDocument() {
var document = DocumentApp.openById("MY_DOC_ID");
var content = document.getBody();
var customerInfo = {
fullName: 'John Smith',
location: '123 Main Street'
};
content.replaceText('{fullName}', customerInfo.fullName);
content.replaceText('{location}', customerInfo.location);
}
The Problem
I have attempted to create a web app version that can handle POST requests:
function doPost(request) {
var content = DocumentApp.openById("ABC123_DocumentID").getBody();
var data = new Function("return " + request.postData.contents)();
content.replaceText('{fullName}', data.fullName);
content.replaceText('{location}', data.location);
return ContentService.createTextOutput("Success");
}
When testing it with this cURL command:
curl -L -d '{"fullName":"Jane Doe","location":"456 Oak Ave"}' 'https://script.google.com/macros/s/SCRIPT_ID/exec'
I’m encountering a “file not found” error. The document contains placeholders such as {fullName} and {location} that are meant to be substituted with the JSON data I am sending.
What I’ve Tried
- I rechecked that the web app option for the script is set up correctly.
- Confirmed that the document ID is accurate.
- Ensured the JSON format sent aligns with what the script is designed to handle.
Could anyone provide insights on what might be leading to this problem?