Example of using Find file/folder action from Google Drive package
- Updated: 2023/11/20
Example of using Find file/folder action from Google Drive package
Build a bot that searches Google Drive for a file and creates the file if it is not found in the specified path.
Prerequisites
- Set up an OAuth2 application in Google Cloud Platform. For more information, see Set up OAuth2 in the Google Cloud Platform.
- This bot uses the Control Room OAuth connection method. Configure OAuth connection in the Control Room. For more information, see Configure OAuth 2.0 connection in Control Room.
- A CSV file with some sample data. If you do not have a CSV file, you can download this file.
Imagine you want to create a Google sheet if it does not exist in Google Drive. You can use the action from the Google Drive package to check if a file already exists in Google Drive. If it does not exist, you can create the file using the Create Workbook action from the Google Sheets package.
This bot checks Google Drive for a Google sheet. If it does not exist in the path specified, the bot creates a Google sheet and copies data from a CSV file to it.
Procedure
-
Create a new bot.
- On the left panel, click Automation.
- Click .
- In the Create Task Bot window, enter a name for the bot.
- Accept the default folder location: \Bots\
- To change the default bot storage location, click Choose and follow the prompts.
- Click Create and edit.
-
Open the CSV file that contains data you wan to copy to a Google Sheet.
- From the Actions panel, find and add action to the Bot editor.
- In Session name, enter csvsession.
- In File path, specify the path to the CSV file from which data should be copied.
- If the CSV table has a header, select Contains header.
- Select a delimiter as per the delimiter used in the CSV file.
- Save the changes.
-
Read data from CSV and store it in a data table variable.
- From the Actions panel, find and add action to the Bot editor.
- In Session name, enter csvsession.
- In Assign value to the variable, click (x) and create a variable called TableFromCSV.
- Save the changes.
-
Close the CSV file.
- From the Actions panel, find and add action to the Bot editor.
- In Session name, enter csvsession.
- Save the changes.
-
Connect to Google Drive.
- From the Actions panel, find and add action to the Bot editor.
- In Username, select Insecure string, and enter the email ID associated with OAuth connection you have set up.
- In OAuth2 Authentication Mode, select Control Room managed.
- Click Pick and select the connection you have configured in Control Room.
- Save the changes.
-
Connect to Google Sheets.
- From the Actions panel, find and add action to the Bot editor.
- In Username, select Insecure string and enter the email ID associated with OAuth connection you have set up.
- In Session name, enter gsheetsession.
- In OAuth2 Authentication Mode, select Control Room managed.
- Click Pick and select the connection you have configured in Control Room.
- Save the changes.
-
Before you create a new sheet in Google Drive, check if the file you are trying
to create already exists. Create the file only if it does not exist in the
specified path. To do this , use the Try Catch actions.
- From the Actions panel, find and add action to the Bot editor.
- Save the changes.
-
To check if the file already exists, use the Google Drive Find file/folder action.
- From the Actions panel, find and add action within the Try block.
- In Username, select Insecure string and enter the email ID associated with the OAuth connection.
- In Find, click File.
- In Source Folder Path, enter the source folder. For example, in this sample scenario, the bot looks for the file in the aa-docs/spreadsheets path.
- In Match type, select Exactly matches.
- In File/Folder name, enter contacts.
- In Returns the list of files or folders, select Dictionary.
- Click (x) , and create a dictionary variable named FileFolderDictionarywith subtype as Any.
- Save changes.
-
If the file does not exist, create the file.
- From the Actions panel, find and add action within the Catch block.
- In Username, select Insecure string and enter the email ID associated with the OAuth connection.
- In Session name, enter gsheetsession.
- In Workbook name, enter contacts.
- In File path, click From my shared location, and enter the parent folder path. For example, in this sample in scenario, the botsearches for the file in the aa-docs/spreadsheets path.
- Save changes.
-
To open a spreadsheet from Google Drive, you need its file ID or the file path.
Since this bot creates the file dynamically, you will not have
the file path prior to the bot execution. However, you can get
the File ID using the Google Drive Find file/folder actionagain.
-
Parse the JSON string associated with the File1 key.
- From the Actions panel, find and add action.
- In Data source, select Text and enter $FileFolderDictionary{File1}$
- In JSON object session, click Local session and enter jsonsession.
- Save the changes.
-
Retrieve file ID from the JSON object.
- From the Actions panel, find and add .
- In JSON node key or path, enter fileId
- In Session name, enter jsonsession.
- In Save the outcome to a variable, click (x) and create a variable called FileId.
- Save the changes.
-
End the JSON session.
- From the Actions panel, find and add action.
- In Session name, enter jsonsession.
- Save the changes.
-
With the retrieved file ID, open the spreadsheet created in Google Drive.
- From the Actions panel, find and add action.
- In Username, select Insecure string and enter the email ID associated with the OAuth connection.
- In Session name, enter gsheetsession
- In Open sheet, click From Spreadsheet Id, and enter $FileId$.
- Save the changes.
-
Write the data table variable to the Google sheet.
- From the Actions panel, find and add action.
- In Session name, enter gsheetsession
- In Sheet options, click Specific Sheet and enter Sheet1 or the sheet name from your target spreadsheet in Sheet name.
- In Enter data table variable, select TableFromCSV
- In Start Cell value, enter A1.
- Save the changes.
-
Disconnect Google Drive and Google Sheets.
- From the Actions panel, find and add action.
- In Username, select Insecure string and enter the email ID associated with the OAuth connection.
- Save changes.
- From the Actions panel, find and add action.
- In Username, select Insecure string and enter the email ID associated with the OAuth connection.
- In Session name, enter gsheetsession
- Save the changes.
- Run the bot.