0

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?.

4
  • At first, I deeply apologize that my answer is not useful for your situation. I have a question for your question. Unfortunately, from I don't know if it will work, I cannot understand about your current issue. I apologize for my poor English skill. Can I ask you about the detail of your current issue? Commented Apr 11, 2021 at 1:14
  • I am developing a web application in Xojo and I use libcURL to communicate with Google Drive. I have a folder ('Templates') with several Google Docs that I use as templates. Depending on the user's choice, I need to combine 2 of those documents. As I can't perform this task directly from cURL, I want to use this function in App Script to combine the documents but I need to call the function from Xojo using cURL so I'm asking for help. Commented Apr 11, 2021 at 10:40
  • Thank you for replying and adding more information. From your updated question, I proposed an answer. Could you please confirm it? If that was not the direction you expect, I apologize. Commented Apr 11, 2021 at 12:35
  • Thank you very much Tanaike, that's exactly what I needed. Finally I used your first cURL command (modified cURL) Commented Apr 11, 2021 at 13:28

1 Answer 1

1

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:

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much Tanaike, that's exactly what I needed. Finally I used your first cURL command (modified cURL)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.