In this post, I demonstrate how to search Google Drive by File Name on Google Sheets using Apps Script.
How to Video:
Video Notes:
Code in Video:
function SearchFileForm()
{
var form = HtmlService.createHtmlOutputFromFile('SearchFile').setTitle('Search Files');
SpreadsheetApp.getUi().showSidebar(form);
}
function addMenu()
{
var menu = SpreadsheetApp.getUi().createMenu('File Search');
menu.addItem('Search Files', 'SearchFileForm');
menu.addToUi();
}
function onOpen(e)
{
addMenu();
}
function searchFiles(title)
{
var ss= SpreadsheetApp.getActiveSpreadsheet();
var filesSheet = ss.getSheetByName("Files");
var lastRow = filesSheet.getLastRow()+1;
filesSheet.getRange('A2:F'+lastRow).clear();
var foundRecords = 'false';
var files = DriveApp.searchFiles("title contains '"+title+"' ");
while (files.hasNext())
{
var file = files.next();
var fileName = file.getName();
var fileLastUpdated = file.getLastUpdated();
var fileCreated = file.getDateCreated();
var fileURL = file.getUrl();
var fileMime = file.getMimeType();
var fileParents = file.getParents();
var firstFolderName = '';
while (fileParents.hasNext()) {
var firstFolderName = fileParents.next().getName();
}
if(firstFolderName != '')
{
filesSheet.appendRow([fileName,
fileURL,
fileCreated,
fileLastUpdated,
fileMime,
firstFolderName]);
}
foundRecords = 'true';
}
if(foundRecords == 'true')
{
return "<span style=\"font-weight: bold\" >Found Records</span>";
}
else
{
return "<span style=\"font-weight: bold\" >No Records Found</span>";
}
}
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />
<script>
function SubmitRecord()
{
document.getElementById("displayReturn").innerHTML = "";
var title = document.getElementById("title").value;
google.script.run.withSuccessHandler(returnBack).searchFiles(title);
}
function returnBack(stringBack)
{
document.getElementById("displayReturn").innerHTML = stringBack;
}
</script>
</head>
<body>
<form>
<div style="padding: 10px" >
<div class="form-row">
<div class="form-group col-md-6">
<label for="title">Title</label>
<input type="text" id="title" class="form-control" />
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<input type="button" value="Submit" onclick="SubmitRecord()" class="btn btn-primary" />
</div>
</div>
</form>
<div id="displayReturn" ></div>
</body>
</html>
Related Posts
![Back Up Google Drive Files](https://codewithcurt.com/wp-content/uploads/2023/04/Back-Google-Drive-Files-WS-150x150.png)
![Embed Chart Google Sheets](https://codewithcurt.com/wp-content/uploads/2022/11/Embed-Chart-GS-WS-150x150.png)
![Create Custom Menu Google Sheets](https://codewithcurt.com/wp-content/uploads/2022/10/Create-Custom-Menu-GS-WS-150x150.png)
![Get and Set Value Google Sheets](https://codewithcurt.com/wp-content/uploads/2022/07/Get-and-Set-Value-GS-WS-150x150.png)
![Create Data Validation Google Sheets](https://codewithcurt.com/wp-content/uploads/2022/04/Create-Data-Validation-GS-WS-150x150.png)
![Stock Trend Analysis Google Sheets](https://codewithcurt.com/wp-content/uploads/2022/04/Stock-Trend-Analysis-GS-WS-150x150.png)
![Scrape Amazon Website Googe Sheets](https://codewithcurt.com/wp-content/uploads/2022/03/Scrape-Amazon-Website-GS-WS-150x150.png)
![Alert Popups on Google Sheets](https://codewithcurt.com/wp-content/uploads/2022/03/Alert-Popups-on-GS-WS-150x150.png)
![Combine Multiple Spreadsheets Google Sheets](https://codewithcurt.com/wp-content/uploads/2022/02/Combine-Multiple-Spreadsheets-GS-WS-150x150.png)
![Last Row and Column Google Sheets](https://codewithcurt.com/wp-content/uploads/2022/04/Last-Row-and-Column-GS-WS-150x150.png)
![Time and Distance Map Google Sheets](https://codewithcurt.com/wp-content/uploads/2022/02/Time-and-Distance-GS-WS-150x150.png)
![Create Dependent Dropdown Google Sheets](https://codewithcurt.com/wp-content/uploads/2022/01/Create-Dependent-Dropdown-GS-WS-150x150.png)
![Call Rest API Google Sheets](https://codewithcurt.com/wp-content/uploads/2022/01/Call-Rest-API-GS-WS-150x150.png)
![Google Doc From Apps Script](https://codewithcurt.com/wp-content/uploads/2021/12/Google-Doc-From-Apps-Script-WS-150x150.png)
![How to Clear, Delete, Insert, and Replace Row Google Sheets](https://codewithcurt.com/wp-content/uploads/2021/12/How-to-Clear-Delete-Insert-and-Replace-Row-GS-WS-150x150.png)
![Email Last Update Google Sheet](https://codewithcurt.com/wp-content/uploads/2021/12/Email-Last-Update-GS-WS-150x150.png)
![JSON Web Service Google Sheets](https://codewithcurt.com/wp-content/uploads/2021/11/Web-Service-GS-WS-150x150.png)
![Scrape Website Google Sheet](https://codewithcurt.com/wp-content/uploads/2021/02/Scrape-Website-GS-WS-150x150.png)
![Query Function Google Sheets](https://codewithcurt.com/wp-content/uploads/2021/02/Query-Function-WS-150x150.png)
![Copy Row Google Sheet to Google Sheet](https://codewithcurt.com/wp-content/uploads/2021/02/Copy-Row-Sheet-to-Sheet-WS-150x150.png)
![Updated Date Column Google Sheet](https://codewithcurt.com/wp-content/uploads/2021/01/Updated-Date-Column-GS-WS-150x150.png)
![](https://codewithcurt.com/wp-content/uploads/2021/01/Create-Google-Sheets-Button-Thumbnail-3-150x150.png)