google-apps-scriptgoogle-docs-apigoogle-workspace-add-ons

Error with Docs.Documents.batchUpdate() but the method throws the error after it completes


I posted recently looking for assistance but ran into a new error that I can't seem to fix.

Exception: Invalid number of arguments provided. Expected 2-3 only

The script is part of a Google Drive Workspace add-on. The user highlights the templates and fills in input on a card on the Drive homepage. Upon clicking apply, the script creates a copy of the template then runs this function to replace all the placeholders.

The code works how it should, replacing all placeholders in the file with data stored in an object for each document id provided. Here is the function that causes the error, it flags this line for the error

let responses = Docs.Documents.batchUpdate(requests);

The function itself is here

function replaceText(copiedTemplateIds) {
  let requests = []
  copiedTemplateIds.forEach(id => {
    requests.push(id)
    for (let [field, data] of Object.entries(enteredInfo)) {
      let placeholder = "<<" + field + ">>"
      requests.push({
            "replaceAllText": {
              "replaceText": data,
              "containsText": {
                "text": placeholder,
                "matchCase": false
              },
            },
          },
      )
    }
  })
  console.log(JSON.stringify(requests))
  let responses = Docs.Documents.batchUpdate(requests);
  console.log(responses.toString())
}

The parameter is an array of template ids. enteredInfo is an object with key:value pairs for the placeholder and user input to replace

I assume the problem is the format of my request but if so, why does it only fail after completing the batchupdate() method correctly?

I can't seem to find any information about this error on SO or Google searches.

I've tried changing the format of the requests, adding {request: {...}}, messing with the placement of the template ID in the array of requests, adding an object {"id": id} and applying replaceAllText to a single document. It always completes the replaceAllText but still gives the error.

If anyone has any ideas on how to rectify this, I would be most appreciative!


Solution

  • Modification points:

    When these points are reflected in your script, it becomes as follows.

    Modified script:

    function replaceText(copiedTemplateIds) {
      copiedTemplateIds.forEach(id => {
        const requests = [];
        for (let [field, data] of Object.entries(enteredInfo)) {
          let placeholder = "<<" + field + ">>";
          requests.push({
            "replaceAllText": {
              "replaceText": data,
              "containsText": {
                "text": placeholder,
                "matchCase": false
              },
            },
          });
        }
        console.log(JSON.stringify(requests));
        let responses = Docs.Documents.batchUpdate({ requests }, id);
        console.log(responses.toString());
        
        // Utilities.sleep(5000); // This might be required to be used.
      });
    }
    

    References: