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!
Docs.Documents.batchUpdate
are resource: Docs_v1.Docs.V1.Schema.BatchUpdateDocumentRequest
and documentId: string
. But, in your script, the argument is only requests
. I think that this is the reason for your current issue.copiedTemplateIds
, if copiedTemplateIds
is The parameter is an array of template ids.
and the Google Document IDs are included in copiedTemplateIds
, unfortunately, in the current stage, multiple Document files cannot be used with one API call. In this case, it is required to use it in a loop.enteredInfo
is not declared. Please be careful about this. In this modification, it supposes that the value of enteredInfo
is like var enteredInfo = { "key1": "value1", "key2": "value2",,, };
.When these points are reflected in your script, it becomes as follows.
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.
});
}
requests
is used for each document ID in copiedTemplateIds
.