เลือกไฟล์และโฟลเดอร์ใน Google ไดรฟ์ด้วย Google Picker

หากต้องการอนุญาตให้ผู้ใช้เลือกไฟล์หรือโฟลเดอร์จาก Google ไดรฟ์ คุณสามารถกำหนดค่า SelectionInput วิดเจ็ตให้ใช้ Google Picker ได้ คู่มือนี้อธิบายวิธีตั้งค่า Google Picker ในการ์ดการกำหนดค่าของส่วนเสริม

กำหนดค่า Google Picker

หากต้องการเปิดใช้เครื่องมือเลือกอินพุตเพื่อเลือกไฟล์หรือโฟลเดอร์จาก Google ไดรฟ์ คุณต้องกำหนดค่า PlatformDataSource ด้วย CommonDataSource และ DriveDataSourceSpec

  1. ตั้งค่า CommonDataSource เป็น DRIVE ซึ่งจะกำหนดให้ Google ไดรฟ์เป็นแหล่งที่มาของอินพุตการเลือก
  2. (ไม่บังคับ) หากต้องการระบุประเภทไฟล์ที่ผู้ใช้เลือกได้ ให้เพิ่ม DriveDataSourceSpec คุณระบุประเภทรายการต่อไปนี้อย่างน้อย 1 รายการได้
    • DOCUMENTS
    • SPREADSHEETS
    • PRESENTATIONS
    • PDFS
    • FORMS
    • FOLDERS

ตัวอย่าง: เลือกสเปรดชีตและไฟล์ PDF

ตัวอย่างต่อไปนี้สร้างการ์ดการกำหนดค่าที่ช่วยให้ผู้ใช้เลือกสเปรดชีตหรือไฟล์ PDF หลายไฟล์จาก Google ไดรฟ์ได้ เมื่อขั้นตอนทำงาน ระบบจะแสดงรหัสไฟล์ของรายการที่เลือกเป็นตัวแปรเอาต์พุต

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