@@ -0,0 +1,3015 @@
+// %5BSTART apps_script_execute%5D%0A/**%0A * Call the API to run an Apps Script function that returns a list%0A * of folders within the user's root directory on Drive.%0A *%0A * @return list of String folder names and their IDs%0A * @throws IOException%0A */%0Aprivate List%3CString%3E getDataFromApi()%0A        throws IOException, GoogleAuthException %7B%0A    // ID of the script to call. Acquire this from the Apps Script editor,%0A    // under Publish %3E Deploy as API executable.%0A    String scriptId = %22ENTER_YOUR_SCRIPT_ID_HERE%22;%0A%0A    List%3CString%3E folderList = new ArrayList%3CString%3E();%0A%0A    // Create an execution request object.%0A    ExecutionRequest request = new ExecutionRequest()%0A            .setFunction(%22getFoldersUnderRoot%22);%0A%0A    // Make the request.%0A    Operation op =%0A            mService.scripts().run(scriptId, request).execute();%0A%0A    // Print results of request.%0A    if (op.getError() != null) %7B%0A        throw new IOException(getScriptError(op));%0A    %7D%0A    if (op.getResponse() != null &&%0A            op.getResponse().get(%22result%22) != null) %7B%0A        // The result provided by the API needs to be cast into%0A        // the correct type, based upon what types the Apps Script%0A        // function returns. Here, the function returns an Apps%0A        // Script Object with String keys and values, so must be%0A        // cast into a Java Map (folderSet).%0A        Map%3CString, String%3E folderSet =%0A                (Map%3CString, String%3E)(op.getResponse().get(%22result%22));%0A%0A        for (String id: folderSet.keySet()) %7B%0A            folderList.add(%0A                    String.format(%22%25s (%25s)%22, folderSet.get(id), id));%0A        %7D%0A    %7D%0A%0A    return folderList;%0A%7D%0A%0A/**%0A * Interpret an error response returned by the API and return a String%0A * summary.%0A *%0A * @param op the Operation returning an error response%0A * @return summary of error response, or null if Operation returned no%0A *     error%0A */%0Aprivate String getScriptError(Operation op) %7B%0A    if (op.getError() == null) %7B%0A        return null;%0A    %7D%0A%0A    // Extract the first (and only) set of error details and cast as a Map.%0A    // The values of this map are the script's 'errorMessage' and%0A    // 'errorType', and an array of stack trace elements (which also need to%0A    // be cast as Maps).%0A    Map%3CString, Object%3E detail = op.getError().getDetails().get(0);%0A    List%3CMap%3CString, Object%3E%3E stacktrace =%0A            (List%3CMap%3CString, Object%3E%3E)detail.get(%22scriptStackTraceElements%22);%0A%0A    java.lang.StringBuilder sb =%0A            new StringBuilder(%22%5CnScript error message: %22);%0A    sb.append(detail.get(%22errorMessage%22));%0A%0A    if (stacktrace != null) %7B%0A        // There may not be a stacktrace if the script didn't start%0A        // executing.%0A        sb.append(%22%5CnScript error stacktrace:%22);%0A        for (Map%3CString, Object%3E elem : stacktrace) %7B%0A            sb.append(%22%5Cn  %22);%0A            sb.append(elem.get(%22function%22));%0A            sb.append(%22:%22);%0A            sb.append(elem.get(%22lineNumber%22));%0A        %7D%0A    %7D%0A    sb.append(%22%5Cn%22);%0A    return sb.toString();%0A%7D%0A// %5BEND apps_script_execute%5D%0A

Migrate Apps Script Execution Snippet