उपयोगकर्ताओं को Google Drive से फ़ाइलें या फ़ोल्डर चुनने की अनुमति देने के लिए, Google Picker का इस्तेमाल करने के लिए SelectionInput विजेट को कॉन्फ़िगर किया जा सकता है. इस गाइड में, आपके ऐड-ऑन के कॉन्फ़िगरेशन कार्ड में Google Picker को सेट अप करने का तरीका बताया गया है.
Google Picker को कॉन्फ़िगर करना
Google Drive से फ़ाइलें या फ़ोल्डर चुनने के लिए, आपको सिलेक्शन इनपुट विजेट को चालू करना होगा. इसके लिए, आपको CommonDataSource के साथ PlatformDataSource और DriveDataSourceSpec को कॉन्फ़िगर करना होगा.
CommonDataSourceकोDRIVEपर सेट करें. इससे Google Drive को, चुनने के लिए इनपुट के सोर्स के तौर पर सेट किया जाता है.- (ज़रूरी नहीं) यह तय करने के लिए कि उपयोगकर्ता किस तरह की फ़ाइलें चुन सकते हैं,
DriveDataSourceSpecजोड़ें. इनमें से एक या एक से ज़्यादा आइटम टाइप तय किए जा सकते हैं:DOCUMENTSSPREADSHEETSPRESENTATIONSPDFSFORMSFOLDERS
उदाहरण: स्प्रेडशीट और PDF फ़ाइलें चुनना
यहां दिए गए उदाहरण में, एक कॉन्फ़िगरेशन कार्ड बनाया गया है. इससे उपयोगकर्ता, Google Drive से एक से ज़्यादा स्प्रेडशीट या PDF फ़ाइलें चुन सकते हैं. यह चरण पूरा होने पर, चुने गए आइटम के फ़ाइल आईडी, आउटपुट वैरिएबल के तौर पर दिखते हैं.
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();
}