@@ -20,49 +20,47 @@ import {GoogleAuth} from 'google-auth-library';
20
20
import { google } from 'googleapis' ;
21
21
22
22
/**
23
- * Call an Apps Script function to list the folders in the user's root Drive
24
- * folder.
23
+ * Calls an Apps Script function to list the folders in the user's root Drive folder.
25
24
*/
26
25
async function callAppsScript ( ) {
26
+ // The ID of the Apps Script project to call.
27
27
const scriptId = '1xGOh6wCm7hlIVSVPKm0y_dL-YqetspS5DEVmMzaxd_6AAvI-_u8DSgBT' ;
28
28
29
- // Get credentials and build service
30
- // TODO (developer) - Use appropriate auth mechanism for your app
29
+ // Authenticate with Google and get an authorized client.
30
+ // TODO (developer): Use an appropriate auth mechanism for your app.
31
31
const auth = new GoogleAuth ( {
32
32
scopes : 'https://www.googleapis.com/auth/drive' ,
33
33
} ) ;
34
+
35
+ // Create a new Apps Script API client.
34
36
const script = google . script ( { version : 'v1' , auth} ) ;
35
- // Make the API request. The request object is included here as 'resource'.
37
+
36
38
const resp = await script . scripts . run ( {
37
39
auth,
38
40
requestBody : {
41
+ // The name of the function to call in the Apps Script project.
39
42
function : 'getFoldersUnderRoot' ,
40
43
} ,
41
44
scriptId,
42
45
} ) ;
43
46
44
47
if ( resp . data . error ?. details ?. [ 0 ] ) {
45
48
// The API executed, but the script returned an error.
46
-
47
- // Extract the first (and only) set of error details. The values of this
48
- // object are the script's 'errorMessage' and 'errorType', and an array of
49
- // stack trace elements.
49
+ // Extract the error details.
50
50
const error = resp . data . error . details [ 0 ] ;
51
-
52
51
console . log ( `Script error message: ${ error . errorMessage } ` ) ;
53
52
console . log ( 'Script error stacktrace:' ) ;
54
53
55
54
if ( error . scriptStackTraceElements ) {
56
- // There may not be a stacktrace if the script didn't start executing .
55
+ // Log the stack trace .
57
56
for ( let i = 0 ; i < error . scriptStackTraceElements . length ; i ++ ) {
58
57
const trace = error . scriptStackTraceElements [ i ] ;
59
58
console . log ( '\t%s: %s' , trace . function , trace . lineNumber ) ;
60
59
}
61
60
}
62
61
} else {
63
- // The structure of the result depends on the Apps Script function's return value.
64
- // Here, the function returns an object with string keys and values, which is
65
- // treated as a Node.js object (folderSet).
62
+ // The script executed successfully.
63
+ // The structure of the response depends on the Apps Script function's return value.
66
64
const folderSet = resp . data . response ?? { } ;
67
65
if ( Object . keys ( folderSet ) . length === 0 ) {
68
66
console . log ( 'No folders returned!' ) ;
0 commit comments