בחירת קבצים ותיקיות ב-Google Drive באמצעות בורר הקבצים של Google

כדי לאפשר למשתמשים לבחור קבצים או תיקיות מ-Google Drive, אפשר להגדיר ווידג'ט של SelectionInput לשימוש בכלי לבחירת קבצים של Google. במדריך הזה מוסבר איך להגדיר את בורר Google בכרטיס ההגדרות של התוסף.

הגדרת הכלי לבחירת קבצים ב-Google

כדי להפעיל את הווידג'ט של קלט הבחירה כדי לבחור קבצים או תיקיות מ-Google Drive, צריך להגדיר את PlatformDataSource שלו עם CommonDataSource ו-DriveDataSourceSpec.

  1. מגדירים את CommonDataSource לערך DRIVE. הפעולה הזו מגדירה את Google Drive כמקור לקלט של הבחירה.
  2. (אופציונלי) כדי לציין אילו סוגי קבצים המשתמשים יכולים לבחור, מוסיפים DriveDataSourceSpec. אפשר לציין אחד או יותר מסוגי הפריטים הבאים:
    • DOCUMENTS
    • SPREADSHEETS
    • PRESENTATIONS
    • PDFS
    • FORMS
    • FOLDERS

דוגמה: בחירת גיליונות אלקטרוניים וקובצי PDF

בדוגמה הבאה מוצג כרטיס הגדרה שמאפשר למשתמשים לבחור כמה גיליונות אלקטרוניים או קובצי PDF מ-Google Drive. כשהשלב מופעל, הוא מחזיר את מזהי הקבצים של הפריטים שנבחרו כמשתני פלט.

JSON

{
  "timeZone": "America/Los_Angeles",
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8",
  "addOns": {
    "common": {
      "name": "Google Picker Demo",
      "logoUrl": "https://www.gstatic.com/images/icons/material/system/1x/pets_black_48dp.png",
      "useLocaleFromApp": true
    },
    "flows": {
      "workflowElements": [
        {
          "id": "file_selection",
          "state": "ACTIVE",
          "name": "File selection",
          "workflowAction": {
            "inputs": [
              {
                "id": "drive_picker_1",
                "description": "Choose a file from Google Drive",
                "dataType": {
                  "basicType": "STRING"
                }
              }
            ],
            "outputs": [
              {
                "id": "file_id",
                "description": "The id of the selected file",
                "cardinality": "SINGLE",
                "dataType": {
                  "basicType": "STRING"
                }
              }
            ],
            "onConfigFunction": "onConfig",
            "onExecuteFunction": "onExecute"
          }
        }
      ]
    }
  }
}

Apps Script

/**
 * Returns a configuration card for the step.
 * This card includes a selection input widget configured as a Google Picker
 * that lets users select spreadsheets and PDFs.
 */
function onConfig() {
  // Specify the file types to allow. In this example, users can
  // select either spreadsheets or PDFs.
  const driveSpec = CardService.newDriveDataSourceSpec()
    .addItemType(CardService.DriveItemType.SPREADSHEETS)
    .addItemType(CardService.DriveItemType.PDFS);

  // Set Google Drive as the data source for the selection input.
  const platformSource = CardService.newPlatformDataSource()
    .setCommonDataSource(CardService.CommonDataSource.DRIVE)
    .setDriveDataSourceSpec(driveSpec);

  // Create the selection input widget.
  const selectionInput =
    CardService.newSelectionInput().setFieldName("drive_picker_1")
      .setPlatformDataSource(platformSource)
      .setTitle("Google Picker")
      .setType(CardService.SelectionInputType.MULTI_SELECT);

  // Build the card section and add the widget.
  var sectionBuilder =
    CardService.newCardSection()
      .addWidget(selectionInput);

  // Build the card and return it.
  var card =
    CardService.newCardBuilder()
      .addSection(sectionBuilder)
      .build();

  return card;
}

/**
 * Executes when the step runs.
 * This function retrieves the file ID of the item selected in the Google Picker
 * and returns it as an output variable.
 * @param {Object} event The event object passed by the Flows runtime.
 * @return {Object} The output variables object.
 */
function onExecute(event) {
  // Get the file ID from the input event object.
  var fileId = event.workflow.actionInvocation.inputs["drive_picker_1"].stringValues[0];

  // Create a variableData object to hold the output.
  const variableData = AddOnsResponseService.newVariableData().addStringValue(fileId);

  // Create a log message for the Activity tab.
  let textFormatElement = AddOnsResponseService.newTextFormatElement().setText("A file has been selected!");
  let workflowTextFormat = AddOnsResponseService.newWorkflowTextFormat().addTextFormatElement(textFormatElement);

  // Create the return action to output the variable. The string key ("result")
  // must match the ID of the output defined in the manifest.
  let returnAction = AddOnsResponseService.newReturnOutputVariablesAction()
      .setVariables({ "file_id": variableData }).setLog(workflowTextFormat);

  // Build and return the render action.
  let hostAppAction = AddOnsResponseService.newHostAppAction().setWorkflowAction(returnAction);

  const renderAction = AddOnsResponseService.newRenderActionBuilder().setHostAppAction(hostAppAction).build();
  return renderAction;
}