Searching in Google Drive to locate a specific folder and file and then inserting a link

I am looking for a way to search and locate specific text within my index spreadsheet that is related to the values in columns A and B. These values are used to create product folders and Google sheets based on a naming convention.

The product folder is named using the formula:

FolderName = ref + ' - ' + location
, which is stored inside a main parent folder. Within this product folder, a Google sheet is created with the name:
newDocName = ref + ' [Descriptions] - ' + location

In the index spreadsheet, I want to automatically populate column L & M with links to the corresponding product folder and sheet file. Is it possible to achieve this directly within the spreadsheet or would I need to use Google Apps Script with an On Open trigger?

If utilizing Google Apps Script is necessary, how can I implement a script to search for and fill in the correct folder and file links in columns L & M? Any guidance would be appreciated.

Thank you.

https://i.sstatic.net/6JoqI.png

Answer №1

Problem Statement:

If my understanding is correct:

  • You have a folder in Google Drive that contains various child folders and files, with titles based on values from columns A and B following specific conventions.
  • Your goal is to retrieve the URLs of these folders and files linked to each row in the spreadsheet and save them in columns L and M respectively.

Solution Approach:

To address this issue, create an Apps Script function that performs the following steps:

Sample Code Snippet:

const SHEET_NAME = "Sheet1"; // Insert your sheet name
const PARENT_FOLDER_ID = "FOLDER_ID"; // Include your parent folder id
function writeUrls() {
  const parentFolder = DriveApp.getFolderById(PARENT_FOLDER_ID);
  const sheet = SpreadsheetApp.getActive().getSheetByName(SHEET_NAME);
  const lastRow = sheet.getLastRow();
  const values = sheet.getRange("A2:B" + lastRow).getValues();
  const urls = values.map(([ref, location]) => {
    const folderName = `${ref} - ${location}`;
    const sheetName = `${ref} [Descriptions] - ${location}`;
    const foldersIter = parentFolder.getFoldersByName(folderName);
    let rowUrls = new Array(2);
    if (foldersIter.hasNext()) {
      const folder = foldersIter.next();
      rowUrls[0] = folder.getUrl();
      const filesIter = folder.getFilesByName(sheetName);
      if (filesIter.hasNext()) rowUrls[1] = filesIter.next().getUrl();
    }
    return rowUrls;
  });
  sheet.getRange("L2:M" + lastRow).setValues(urls);
}

Answer №2

If you're looking to achieve your goal, the Drive API is a great choice. You can activate the Drive API in your script and execute the Drive.Files.list() method with a search query. Here's an example:

Drive.Files.list({q: "fullText contains 'Example One'"});

For more details and additional options, check out the search query guide.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Learn the easy way to automatically generate HTML in a Vue template file

Is there a way to create HTML inside a Vue template without having to do it manually using the VS Code editor? <template> <div class="hello"> <h1>Hello, world!</h1> </div> </template> <script> export de ...

Embarking on your journey with the Withings API

How can I connect to my website with the Withings API? I want to send weight values to the Withings API and receive body measurement values. Where can I find the code for the Withings API? $config['withings_settings']['widgets'] = &apo ...

Intersecting realms document

Currently I am working on a web store using Magento Go. Unfortunately, this platform does not support server side scripting languages such as PHP. Despite this limitation, I still need to save order data post successful checkout and share it with my shippi ...

Disabling the experimental app directory feature in NextJs

I've been working on a real-life project and decided to test out the new App directory feature that comes with Next.js version 13. However, I encountered some issues such as images and fonts not loading properly. Now, I'm looking to opt out of th ...

Changing color based on AngularJS condition using CSS

My HTML code looks like this: <div> <i class="glyphicon glyphicon-envelope" style="margin-top: -50px; cursor:pointer; color: #1F45FC" ng-click="createComment(set_id)"> </i> Route <center> ...

i18Next: utilize specific locales for certain languages and combine language with location for all others

Query regarding the i18next library: I am working with a selection of locales that can be detected from the browser, de en-US es fr it ja ko pt-br zh-cn zh-tw Is there a way to configure it so that it respects the language for specific locales only? For i ...

Local AngularJS template fails to load, yet loads successfully on Plnkr platform

Currently, I am experimenting with a proof of concept on my local machine using this plnkr: http://plnkr.co/edit/at8SXmvb2LkWYtuZE9Vm?p=preview While everything functions correctly in plnkr, the issue arises when I download the zip file and attempt to ru ...

Extend the start day by one day for the checkout option on the Pikaday datepicker

Is there a way to automatically add 1 day to the checkout date when selecting a check-in date? For example, if today is November 8, 2017 and I select November 10 as the check-in date, can the checkout date automatically adjust to November 11? This is the ...

Having issues with Cypress testing of Material-UI datepicker on Github actions

Encountering an unusual issue while running Cypress tests in a GitHub action environment. The MUI datepicker is stuck in readonly mode, preventing any input of dates (works fine in other setups). Error displayed by Cypress CypressError: Timed out retryin ...

Issues with resetting AngularJS form---"The AngularJS form

I have been putting in a lot of effort to make this work. Despite my knowledge about child scopes, prototypal inheritance, and the use of dot notation for the model, I am facing an issue with resetting the form. You can access the hosted form here. The rel ...

Convert components into <input> using the "Edit" button, and store information with the "Save" button

My HTML script looks like this: <ul id="data"> <li>Value 1</li> <li>Value 2</li> <li>Value 3</li> <li>Value 4</li> </ul> <br> <div id="buttons"> ...

Angular Datepicker MinDate function prevents selection of dates "behind" by one

I've encountered an issue with the Angular Bootstrap Datepicker where I'm setting the min-date attribute as follows: <input type="text" class="" uib-datepicker-popup="MM/dd/yyyy" show-button-bar="false" ng-model="vm.date" ng-change= ...

"Populate a div with an array of images extracted from a JSON file

I found a website that provides an array of images like the sample below. { "imageUrl":"http://server06.amobee.com/aspx/nadav/test/banner320x50.png", "expandedImageUrl":"http://server06.amobee.com/aspx/nadav/test/banner320x320.jpg" } My ob ...

Unable to configure raycaster layers within Three.js framework

While attempting to configure the raycaster layer to only cast on a single layer, as outlined in the threejs documentation: - I encountered the following error Uncaught TypeError: Cannot read properties of undefined (reading 'set') What could b ...

When using the CDN import version, React does not render components or code

Unfortunately, I am unable to progress with studying React due to encountering a basic issue. Please excuse my lack of experience, no need to judge harshly. The message is not displaying, although the code itself is straightforward; I am perplexed as to wh ...

Manipulator - Unveiling Discord User Profile Popup

Hey there, I'm currently working on a web project and I want to send messages in a Discord server using Puppeteer without relying on the Discord.js library. While I have successfully set up user authentication and navigated to the correct chat room, I ...

Angular animations are failing to function in the Visual Studio 2017 template

I am facing a challenge with incorporating animations in Angular 2 using Visual Studio 2017. I believe there must be a simple solution that I am overlooking at the moment. My goal is to animate certain elements and I have been testing the animations by tri ...

Using jQuery and regex to only allow alphanumeric characters, excluding symbols and spaces

Seeking advice, I am using a jquery function called alphanumers var alphanumers = /^[a-zA-Z0-9- ]*$/; which currently does not allow all symbols. However, I now wish to disallow the space character as well. Any suggestions? ...

What is the most efficient method for swapping out a portion of an array with a different one in Javascript?

Currently experimenting with Dygraph and have successfully integrated a reloader feature that adds high-resolution data as users zoom in on the graph. To maintain the original file boundaries, I am preserving the existing data and inserting the newly load ...

Difficulty encountered when attempting to modify textures in three.js

Trying to apply a texture to a mesh in three.js: Initializing variables: var container, stats; var camera, scene, projector, raycaster, renderer; var mouse = new THREE.Vector2(), INTERSECTED; onMouseDownMouseX = 0, onMouseDownMouseY ...