Wardiam
Wardiam

Reputation: 197

Calling a Google App Script function using the API and cURL

I apologise if this question is too simple but I am a newbie to using cURL. I have created a function in Google App Script to merge text from two Google Docs. The function is this:

function mergeGoogleDocs(doc1, doc2) {

var BaseDoc = DocumentApp.openById(doc1);
var body = BaseDoc.getActiveSection();

var otherBody = DocumentApp.openById(doc2).getActiveSection();
var totalElements = otherBody.getNumChildren();
for( var j = 0; j < totalElements; ++j ) {
var element = otherBody.getChild(j).copy();
var type = element.getType();
if( type == DocumentApp.ElementType.PARAGRAPH )
  { 
  body.appendParagraph(element); 
  }
else if( type == DocumentApp.ElementType.TABLE )
{
body.appendTable(element);
}
else if( type == DocumentApp.ElementType.LIST_ITEM )
{
body.appendListItem(element);
}
else
{
throw new Error("Error de Formato: "+type);
}
}

}

I have tested the script and it works perfectly if I put the two document-IDs. I would like to be able to call this function from outside Google using cURL. For this I have enabled all the necessary (credentials, scope permissions, project number and implemented it as an executable API). I've been looking for a way to run the function with cURL and only found this in this post:

curl -X POST -L \
    -H "Authorization: Bearer ### access token ###" \
    -H "Content-Type: application/json" \
    -d "{function: '### function name ###',devMode: true}" \
    "https://script.googleapis.com/v1/scripts/### script ID ###:run"

I need to include the two document_IDs as parameters therefore based on the information in the previous post I have created the following sequence:

curl -X POST -L \
-H 'Authorization: Bearer ##access token##' \
-H "Content-Type: application/json" \
-d '{"function": "##function name##", "parameters": {"doc1":"##docID1##,"doc2":"##docID2##"}, devMode: true}' \ 
"https://script.googleapis.com/v1/scripts/### script ID ###:run"

But when I run the sequence I get this error message:

{
  "done": true,
  "error": {
    "code": 3,
    "message": "ScriptError",
    "details": [
      {
        "@type": "type.googleapis.com/google.apps.script.v1.ExecutionError",
        "scriptStackTraceElements": [
          {
            "function": "##function name##",
            "lineNumber": 3
          }
        ],
        "errorMessage": "Exception: Document is missing (perhaps it was deleted, or you don't have read access?)",
        "errorType": "ScriptError"
      }
    ]
  }
}

Could someone please help me to put the sequence correctly for the App Script to work?.

Upvotes: 0

Views: 730

Answers (1)

Tanaike
Tanaike

Reputation: 201603

I believe your goal and your current situation as follows.

  • You want to give the values of ##docID1## and ##docID2## to the function of mergeGoogleDocs(doc1, doc2) in Google Apps Script project.
  • You want to run the function using a CURL command.
  • In your situation, the preparation for using the method of "scripts.run" in Google Apps Script API has already been finished.
  • Your Google Apps Script works fine.

Modification points:

  • In order to give 2 arguments to function mergeGoogleDocs(doc1, doc2) {}, please give the values of value of ##docID1## and ##docID2## to the array of parameters like "parameters": ["##docID1##", "##docID2##"]. In this case, when "parameters": ["##docID1##", "##docID2##"] is used, each element of the array is corresponding to each argument for the function in Google Apps Script.
  • For example, you want to give the values of ##docID1## and ##docID2## to the function as the arguments of mergeGoogleDocs(doc1, doc2), please set the value like "parameters": ["##docID1##", "##docID2##"].
  • By the way, in your second curl command, when {"doc1":"##docID1##,"doc2":"##docID2##"} is used, "##docID1## is not enclosed.

When above points are reflected to your curl command, it becomes as follows.

Modified curl command:

curl -X POST \
-H 'Authorization: Bearer ### access token ###' \
-H "Content-Type: application/json" \
-d '{"function": "##function name##", "parameters": ["##docID1##", "##docID2##"], devMode: true}' \
"https://script.googleapis.com/v1/scripts/### script ID ###:run"

Note:

  • When you want to use {"doc1":"##docID1##","doc2":"##docID2##"} as the arcuments, you can also use the following curl command.

      curl -X POST \
      -H 'Authorization: Bearer ### access token ###' \
      -H "Content-Type: application/json" \
      -d '{"function": "##function name##", "parameters": [{"doc1":"##docID1##","doc2":"##docID2##"}], devMode: true}' \
      "https://script.googleapis.com/v1/scripts/### script ID ###:run"
    
    • In this case, please modify your script at Google Apps Script side as follows.

      • From

          function mergeGoogleDocs(doc1, doc2) {
        
      • To

          function mergeGoogleDocs({doc1, doc2}) {
        
  • When the argument is only one, it seems that "parameters": "value" can be used. Namely, I confirmed that the result of "parameters": "value" is the same with the result of "parameters": ["value"].

Reference:

Upvotes: 1

Related Questions