Chọn tệp và thư mục trên Google Drive bằng Google Picker

Để cho phép người dùng chọn tệp hoặc thư mục trong Google Drive, bạn có thể định cấu hình một tiện ích SelectionInput để sử dụng Google Picker. Hướng dẫn này giải thích cách thiết lập Google Picker trong thẻ cấu hình của tiện ích bổ sung.

Định cấu hình Bộ chọn của Google

Để cho phép tiện ích đầu vào lựa chọn chọn tệp hoặc thư mục trên Google Drive, bạn phải định cấu hình PlatformDataSource của tiện ích này bằng CommonDataSourceDriveDataSourceSpec.

  1. Đặt CommonDataSource thành DRIVE. Thao tác này chỉ định Google Drive làm nguồn cho dữ liệu đầu vào lựa chọn.
  2. (Không bắt buộc) Để chỉ định những loại tệp mà người dùng có thể chọn, hãy thêm một DriveDataSourceSpec. Bạn có thể chỉ định một hoặc nhiều loại mục sau:
    • DOCUMENTS
    • SPREADSHEETS
    • PRESENTATIONS
    • PDFS
    • FORMS
    • FOLDERS

Ví dụ: Chọn bảng tính và tệp PDF

Ví dụ sau đây tạo một thẻ cấu hình cho phép người dùng chọn nhiều bảng tính hoặc tệp PDF trên Google Drive. Khi chạy, bước này sẽ trả về mã nhận dạng tệp của các mục đã chọn dưới dạng biến đầu ra.

JSON

{
  "timeZone": "America/Los_Angeles",
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8",
  "addOns": {
    "common": {
      "name": "Drive 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": "fileId",
                "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() {
  // Allows users to select either spreadsheets or PDFs
  const driveSpec = CardService.newDriveDataSourceSpec()
    .addItemType(
      CardService.DriveItemType.SPREADSHEETS
    )
    .addItemType(
      CardService.DriveItemType.PDFS
    );

  const platformSource = CardService.newPlatformDataSource()
    .setCommonDataSource(
      CardService.CommonDataSource.DRIVE
    )
    .setDriveDataSourceSpec(driveSpec);

  const selectionInput =
    CardService.newSelectionInput()
      .setFieldName("drive_picker_1")
      .setPlatformDataSource(platformSource)
      .setTitle("Drive Picker")
      .setType(
        CardService.SelectionInputType.MULTI_SELECT
      );

  var sectionBuilder =
    CardService.newCardSection()
      .addWidget(selectionInput)

  return CardService.newCardBuilder()
    .addSection(sectionBuilder)
    .build();
}

/**
* 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) {
  //Extract the selected file's ID during execution
  console.log("eventObject: " + JSON.stringify(event));
  var fileId = event.workflow.actionInvocation.inputs["drive_picker_1"].stringValues[0];

  const variableData = AddOnsResponseService.newVariableData()
    .addStringValue(fileId);

  let textFormatElement = AddOnsResponseService.newTextFormatElement()
    .setText("A file has been selected!");

  let workflowTextFormat = AddOnsResponseService.newWorkflowTextFormat()
    .addTextFormatElement(textFormatElement);

  let workflowAction = AddOnsResponseService.newReturnOutputVariablesAction()
    .addVariableData("fileId", variableData)
    .setLog(workflowTextFormat);

  let hostAppAction = AddOnsResponseService.newHostAppAction()
    .setWorkflowAction(workflowAction);

  return AddOnsResponseService.newRenderActionBuilder()
    .setHostAppAction(hostAppAction)
    .build();
}