انتخاب فایل‌ها و پوشه‌های گوگل درایو با استفاده از Google Picker

برای اینکه کاربران بتوانند فایل‌ها یا پوشه‌ها را از گوگل درایو انتخاب کنند، می‌توانید یک ویجت SelectionInput را برای استفاده از Google Picker پیکربندی کنید. این راهنما نحوه تنظیم Google Picker را در کارت پیکربندی افزونه شما توضیح می‌دهد.

پیکربندی انتخابگر گوگل

برای فعال کردن ویجت ورودی انتخاب برای انتخاب فایل‌ها یا پوشه‌ها از گوگل درایو، باید PlatformDataSource آن را با CommonDataSource و DriveDataSourceSpec پیکربندی کنید.

  1. مقدار CommonDataSource را روی DRIVE تنظیم کنید. این کار Google Drive را به عنوان منبع ورودی انتخاب تعیین می‌کند.
  2. (اختیاری) برای مشخص کردن نوع فایل‌هایی که کاربران می‌توانند انتخاب کنند، یک DriveDataSourceSpec اضافه کنید. می‌توانید یک یا چند نوع از موارد زیر را مشخص کنید:
    • DOCUMENTS
    • SPREADSHEETS
    • PRESENTATIONS
    • PDFS
    • FORMS
    • FOLDERS

مثال: انتخاب صفحات گسترده و فایل‌های PDF

مثال زیر یک کارت پیکربندی ایجاد می‌کند که به کاربران اجازه می‌دهد چندین فایل صفحه گسترده یا PDF را از Google Drive انتخاب کنند. وقتی این مرحله اجرا می‌شود، شناسه‌های فایل موارد انتخاب شده را به عنوان متغیرهای خروجی برمی‌گرداند.

جی‌سون

{
  "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"
          }
        }
      ]
    }
  }
}

اسکریپت برنامه‌ها

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