3

How should I approach the problem of saving a file to a specific folder depending on the result of a variable?

For example,

Say I have 3 clients Bob, Billy and Ben. And on google drive, I have corresponding folders for each client, so "BobFolder", "BillyFolder" and "BenFolder".

If the name happens to be "Bob" during a loop, then I would like to save my file to "BobFolder". Since I am using DriveApp.getFolderById(), I am thinking the only approach is to use switch statements (or similar logic) using the folder IDs. Or is there another way to do this?

Appreciate any help I can get!

Here is roughly how I would grab the client name from my google sheet:

// get data range
  const rows = invSh.getDataRange().getValues();

  var toIndex = getColumnId(invSh, "To") - 1;

rows.forEach(function (row, index) {
  // skip header row logic

// skip rows that already have document created and linked logic

    // make copy document of template and get body (this is also where i would add logic to figure out the destination folder depending on the client)
    const copy = googleDocTemplate.makeCopy(`INVOICE_${row[invNumIndex]}`, destinationFolder);
    const doc = DocumentApp.openById(copy.getId());
    const body = doc.getBody();

// add client name to doc
body.replaceText('{{to}}', row[toIndex]);
}

So during the loop, each row will call row[toIndex] which would could for example be:

row[0] -> 'Bob'

row[1] -> 'Billy'

etc.

dan
  • 347
  • 2
  • 14
  • I thought that in your question, more information might help users think of a solution or a workaround. For example, about `If the name happens to be "Bob" during the function call`, in your situation, how is the script run? How are you confirming the user name? – Tanaike Sep 13 '22 at 05:25
  • @Tanaike Current script runs like so: 1) Get template doc using ID 2) Set destination folder using ID 3) Get sheet data range and cycle through each row of data 4) Create new document for each row using template and populate it using data from sheet Each row in the sheet contains data such as client name, invoice number, etc. I am able to get the client's name from the row array e.g. var name = row[2] The script itself is called via a menu that is created onOpen – dan Sep 13 '22 at 07:24
  • @Tanaike My code is follows a similar flow to this https://jeffreyeverhart.com/2020/09/29/auto-fill-a-google-doc-template-from-google-sheet-data/ – dan Sep 13 '22 at 07:26
  • Thank you for replying. From your reply, although unfortunately, I cannot imagine your whole script, when your script is run, when you can retrieve the user name using `var name = row[2]`, how about selecting the folder name using the user name when the script is run? When the folder names in your question are used, how about `var folderName = ["BobFolder", "BillyFolder", "BenFolder"].find(e => e.includes(userName));`? In this script, when the user name is converted to the folder name. If I misunderstood your reply, I apologize. – Tanaike Sep 13 '22 at 07:35
  • Consider clarifying how you get the client name (e.g. Bob). I'm not sure what you mean by `If the name happens to be "Bob" during the function call`. Also, it would be useful to provide the relevant parts of your code. It's not clear how the code you referenced is related to the issue you described. – Iamblichus Sep 13 '22 at 10:06
  • @lamblichus i have updated my question to include a snippet of the code; tried to trim it down as much as possible without removing the relevant parts. let me know if this helps – dan Sep 13 '22 at 15:17

2 Answers2

1

Do you mean something like this?

var IDs = {
    'Bob':  'ID-1',
    'Bill': 'ID-2',
    'Ben':  'ID-3',
}

var folder_id = IDs['Bill'];

console.log(folder_id); // output: ID-2

var the_name = 'Bob';
folder_id = IDs[the_name];

console.log(folder_id); // output: ID-1

And you can use:

var dest_folder = DriveApp.getFoldersByName('BobFolder').next();

But make sure that you have just one folder with name 'BobFolder' on your Drive. Otherwise the result could be random. This is why to use ID is a better idea.

Yuri Khristich
  • 13,448
  • 2
  • 8
  • 23
  • Ah, so it is possible to get folders by name, just not recommended? If it is safer to use IDs, I guess I'll just have to update the code whenever I have a new client (and therefore a new folder). – dan Sep 13 '22 at 15:23
  • Exactly: https://developers.google.com/apps-script/reference/drive/drive-app#getfoldersbynamename – Yuri Khristich Sep 13 '22 at 16:57
1

If you want to find the folder by its name, you would do this:

const clientName = row[toIndex]; // e.g. Bob
const folderName = `${clientName}Folder`;
const folderIter = DriveApp.getFoldersByName(folderName);
if (folderIter.hasNext()) {
  const destinationFolder = folderIter.next();
  // Rest of your code
}

As Yuri Khristich said, though, this might not point to the right folder if there are multiple folders with that name in your Drive. In this case, using the folder id might be a better approach (see getFolderById).

Also, if all your possible destination folders are children of a given folder, you could use Folder.getFoldersByName(name) instead. This could help if you have other folders with your desired names outside that specific parent folder.

Iamblichus
  • 18,540
  • 2
  • 11
  • 27