[英]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.