[英]Searching for a Google-apps-script to iterate through a folder of sheets and append a range of data to a consolidated master sheet
[英]Google Apps Script: Creating a document into a specific folder by searching folder name from data in a Google Sheet?
情况:我在 Google 表格上有数据。 我正在使用该数据填写模板 Google Doc,并且新创建的 Doc 需要根据两个条件保存到特定的 Google Drive 文件夹中。 条件是名称和月份,它们都是工作表上的变量。
问题:我不知道如何将文档保存在正确的位置,或者是否可以使用 Google Apps 脚本/Google Drive。 我的工作表中的两列包含一个人的姓名和月份的数据。 我想首先按名称搜索文件夹,然后在该文件夹中,我想使用工作表中的行数据按月份搜索。 该文档将保存到 Name 文件夹内的这个 Month 子文件夹中。
当前状态:我的代码从工作表中提取数据,将其插入到模板中,并将新文档命名为我告诉它的名称。 我能够告诉它在哪里保存这个新文档
const destinationFolder = DriveApp.getFolderById('ID_GOES_HERE');
但是有多个名称和多个月份,所以我不能对所有这些都使用 ID。 我希望代码根据工作表中的名称和月份变量动态找到正确的子文件夹。
这甚至可能吗?
这是我当前代码的一个片段,为了匿名,我删除了 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);
});
}
为将来遇到类似问题的任何人编辑,这是我的结果! 感谢迈克和尤里。
const parent_folder = DriveApp.getFoldersByName(row[3]).next();
const subfolder = parent_folder.getFoldersByName(row[2]).next();
const destinationFolder = DriveApp.getFoldersByName(subfolder).next();
尝试
const destinationFolder = DriveApp.getFolderById(getChildId(getParentId(row[4]),row[2]));
并添加功能(他们将创建需要的文件夹)
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.