Updating Google Docs placeholders using cURL and Apps Script

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?

The ‘file not found’ error likely stems from your deployment settings rather than your code. It’s a common pitfall with Apps Script web apps. Ensure you’ve set the deployment correctly: click on ‘Deploy’ > ‘New deployment’, select ‘Web app’, and verify that ‘Execute as’ is set to ‘Me’, with access permissions set to ‘Anyone’ or ‘Anyone, even anonymous’. Also, make sure to create a new deployment version after updating your doPost function, as simply saving won’t suffice. Additionally, I recommend replacing the new Function() method for parsing JSON with JSON.parse(request.postData.contents) for better reliability. Finally, ensure you’re testing with the correct deployment URL that you find in the deployment section, as it should end with ‘/exec’.

This screams deployment permissions issue. Double-check you’re using the latest version number when deploying - Apps Script won’t automatically grab your newest code unless you redeploy. Throw some console.log() statements in your doPost function to see if it’s even firing. Half the time these error messages are BS and the function’s actually running but dying somewhere else.

I’ve hit this exact problem - super frustrating. The issue’s probably your JSON parsing. Don’t use new Function() for JSON - it’s risky and fails silently. Switch to JSON.parse() wrapped in try-catch to handle bad data. Also check your cURL command is sending the Content-Type header: add -H \"Content-Type: application/json\". Google Apps Script caches responses like crazy, so after changing your doPost function, wait a few minutes or clear your browser cache before testing. The document ID issue might be permissions - make sure your script can access the document when running as a web app.