Search code examples
curlgoogle-apps-scriptgoogle-apps-script-api

Add array as parameter in Google Apps Script function using cURL


I have created a function in Google Apps Script to create and manage several items in Google Drive (e.g. create a folder, a new Doc or Xls file, etc.). The important thing is that I have 4 DOC documents (or templates): MainDoc + 3 additional Doc files. With my script the MainDoc file is duplicated and renamed (step1) and then in a second step the array is defined with the IDs of the 3 additional files that are going to be added/combining their texts on the MainDoc (step2). To make my explanation understandable, I show the basic structure of my function:

1    function GlobalWorkflow(ProjectID, mergeDocsID) {
2    ...
3    //### STEP1 ###//
4      var newFolderID = parentFolder.createFolder(date + '_Report').getId();
5      var newFolder = DriveApp.getFolderById(newFolderID);  
6      var docTemplateID = "##MainDocID##";
7      var getDOCTemplate = DriveApp.getFileById(docTemplateID);
8      var MainDocID = getDOCTemplate.makeCopy(date + '_Report', newFolder).getId();
9    ...
10    //### STEP2 ###//
11      //Input: documentIDs to merge
12      //var mergeDocsID = "##Doc2ID##|##Doc3ID##|##Doc4ID##"
13      var mergeArray = mergeDocsID.split('|');
14      for (var i = 0; i < mergeArray.length; i++) {
15      mergeGoogleDocs(MainDocID, mergeArray[i]);
16     }
17    ...
18    }

Line 15 calls another function 'mergeGoogleDocs' which is fed with 2 parameters. This other function was described in this other post and thanks to @Tanaike I managed to run this function from cURL using this 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"

The problem I have now is that I want to run the 'GlobalWorkflow' function but one of the parameters (mergeDocsID) is the array with the IDs of the doc files to combine and I get the whole script to work from cURL except that part (Step2) although the response doesn't give me an error.

This is the command I used:

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

How could I include the array in the cURL command to make the 'mergeGoogleDocs' function work?.

PS. If I uncomment line 12 and run the function from GAS everything works perfectly and the combined document with the 4 files is created. What doesn't work is the cURL command.

Could anyone help me?.

Thank you very much.

Wardiam


Solution

  • I believe your goal and your current situation as follows.

    • You want to give an array like ["##Doc2ID##","##Doc3ID##","##Doc4ID##"] to mergeDocsID of GlobalWorkflow(ProjectID, mergeDocsID) using a curl command.
    • You have already confirmed that when the correct values are given to the function GlobalWorkflow, the script worked fine.
    • You have already been able to run a function using Google Apps Script API.

    In order to give the array like ["##Doc2ID##","##Doc3ID##","##Doc4ID##"], please modify your curl command as follows.

    From:

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

    To:

    curl -X POST \
    -H 'Authorization: Bearer ### access token ###' \
    -H "Content-Type: application/json" \
    -d '{"function": "##function name##", "parameters": ["##MainDocID##", ["##Doc2ID##","##Doc3ID##","##Doc4ID##"]], devMode: true}' \
    "https://script.googleapis.com/v1/scripts/### script ID ###:run"
    
    • By above modification, an array of ["##Doc2ID##","##Doc3ID##","##Doc4ID##"] is given as mergeDocsID of GlobalWorkflow(ProjectID, mergeDocsID). So at Google Apps Script side, when above modified curl command is run and mergeDocsID[0] is checked, "##Doc2ID##" is returned.

    Reference: