使用 Google 选择器选择 Google 云端硬盘文件和文件夹

如需允许用户从 Google 云端硬盘中选择文件或文件夹,您可以配置 SelectionInput widget 以使用 Google 选择器。本指南介绍了如何在插件的配置卡片中设置 Google 选择器。

配置 Google 选择器

如需让选择输入 widget 能够从 Google 云端硬盘中选择文件或文件夹,您必须使用 CommonDataSourceDriveDataSourceSpec 配置其 PlatformDataSource

  1. CommonDataSource 设置为 DRIVE。这会将 Google 云端硬盘指定为选择输入的来源。
  2. (可选)如需指定用户可以选择哪些文件类型,请添加 DriveDataSourceSpec。您可以指定以下一种或多种商品类型:
    • DOCUMENTS
    • SPREADSHEETS
    • PRESENTATIONS
    • PDFS
    • FORMS
    • FOLDERS

示例:选择电子表格和 PDF

以下示例构建了一个配置卡片,可让用户从 Google 云端硬盘中选择多个电子表格或 PDF 文件。当此步骤运行时,它会以输出变量的形式返回所选文件的 ID。

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 脚本

/**
 * 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;
}