[英]Google Apps Script: Creating a document into a specific folder by searching folder name from data in a Google Sheet?
The Situation: I have data on a Google Sheet.情况:我在 Google 表格上有数据。 I am using that data to fill in a template Google Doc, and the newly-created Doc needs to be saved into a specific Google Drive folder based on two criteria.我正在使用该数据填写模板 Google Doc,并且新创建的 Doc 需要根据两个条件保存到特定的 Google Drive 文件夹中。 The criteria are a Name and a Month, both of which are variables on the Sheet.条件是名称和月份,它们都是工作表上的变量。
The Problem: I can't figure out how to save the doc in the right place, or if it's even possible with Google Apps Script / Google Drive.问题:我不知道如何将文档保存在正确的位置,或者是否可以使用 Google Apps 脚本/Google Drive。 Two of the columns in my Sheet have data with a person's Name and Month.我的工作表中的两列包含一个人的姓名和月份的数据。 I want to search first for a folder by Name, and then within that folder, I want to search by Month using the row data from the Sheet.我想首先按名称搜索文件夹,然后在该文件夹中,我想使用工作表中的行数据按月份搜索。 The doc would be saved into this Month subfolder within the Name folder.该文档将保存到 Name 文件夹内的这个 Month 子文件夹中。
Current State: My code pulls data from the Sheet, inserts it into the template, and names the new doc what I tell it to.当前状态:我的代码从工作表中提取数据,将其插入到模板中,并将新文档命名为我告诉它的名称。 I was able to tell it where to save this new doc using我能够告诉它在哪里保存这个新文档
const destinationFolder = DriveApp.getFolderById('ID_GOES_HERE');
but there are multiple Names and multiple Months, so I can't use IDs for all of them.但是有多个名称和多个月份,所以我不能对所有这些都使用 ID。 I want the code to dynamically find the correct subfolder based on Name and Month variable in the Sheet.我希望代码根据工作表中的名称和月份变量动态找到正确的子文件夹。
Is this even possible?这甚至可能吗?
Here's a snip of my current code, I removed IDs and such for anonymity:这是我当前代码的一个片段,为了匿名,我删除了 ID 等:
/* Creates a menu item in the Spreadsheet that triggers population of the coaching package documents. */
function onOpen() {
const ui = SpreadsheetApp.getUi();
const menu = ui.createMenu('AutoFill Template');
menu.addItem('Create New Document', 'createNewGoogleDocs');
menu.addToUi();
}
/* Loops through spreadsheet rows. If there is no Document Link, a new document will be generated. */
function createNewGoogleDocs() {
const googleDocTemplate = DriveApp.getFileById('ID_GOES_HERE');
const destinationFolder = DriveApp.getFolderById('ID_GOES_HERE');
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Responses');
const rows = sheet.getDataRange().getValues();
rows.forEach(function(row, index) {
if (index === 0) return;
if (row[13]) return;
const copy = googleDocTemplate.makeCopy(`QCP - ${row[4]} - ${row[2]}`, destinationFolder);
const doc = DocumentApp.openById(copy.getId());
const body = doc.getBody();
body.replaceText('{{Title Name}}', row[4]);
body.replaceText('{{Month}}', row[2]);
body.replaceText('{{Name}}', row[3]);
body.replaceText('{{Desired 1}}', row[5]);
body.replaceText('{{Desired 2}}', row[6]);
doc.saveAndClose();
const url = doc.getUrl();
sheet.getRange(index + 1, 14).setValue(url);
});
}
EDIT for anyone who has a similar problem in the future, this was my result!为将来遇到类似问题的任何人编辑,这是我的结果! Thank you to both Mike and Yuri.感谢迈克和尤里。
const parent_folder = DriveApp.getFoldersByName(row[3]).next();
const subfolder = parent_folder.getFoldersByName(row[2]).next();
const destinationFolder = DriveApp.getFoldersByName(subfolder).next();
Basically you can get a folder by a name this way:基本上你可以通过这种方式获得一个文件夹:
const destinationFolder = DriveApp.getFoldersByName(name).next();
To get a subfolder within a parent folder it could be somethgn like this:要在父文件夹中获取子文件夹,可能是这样的:
const parent_folder = DriveApp.getFoldersByName(name).next();
const subfolder = parent_folder.getFoldersByName(name).next();
Try with尝试
const destinationFolder = DriveApp.getFolderById(getChildId(getParentId(row[4]),row[2]));
and add functions (they will create the folders in needed)并添加功能(他们将创建需要的文件夹)
function getParentId(name){
// create if absent in drive
var id;
var folders = DriveApp.getFoldersByName(name);
if (folders.hasNext()) {
var folder = folders.next();
id = folder.getId(); }
else {
var folder = DriveApp.createFolder(name);
id = folder.getId();
}
return id;
}
function getChildId(parentId,name){
// create if absent in parent folder
var parent = DriveApp.getFolderById(parentId);
var id;
var folders = parent.getFoldersByName(name);
if (folders.hasNext()) {
var folders = folders.next();
id = folder.getId(); }
else {
var folder = parent.createFolder(name);
id = folder.getId();
}
return id;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.