Google apps script 谷歌应用程序脚本复制和重命名文件夹结构,包括更新文件中的字段和文件夹中包含的工作表?
我必须首先道歉,因为我以前从未做过任何编码。我一直在用谷歌搜索和查看其他人的代码,试图找出到底发生了什么。我有逻辑,但我不知道所有的类、触发器,甚至不知道正确的术语!我可以在脑海中勾勒出需要发生什么,但首先需要学习语言,所以希望你能为我指出学习材料的正确方向,并帮助我完成这个特定的项目 我想自动化我们办公室经常做的事情,即创建一个包含预定义子文件夹的新客户文件夹,获取一个模板文档和工作表,填写名称和地址字段,并将其与客户名称一起保存到新复制/创建的相关文件夹中。我正在努力连贯地解释这一点,所以我画了一些东西,希望能有所帮助: 我制作了一个谷歌表单,将结果发布到电子表格中,并在该表单中添加了一个脚本。我已经解决了如何获取模板表和文档,将它们复制并重命名到文件夹中,但我不知道如何处理这些文件夹,以便将它们归档到驱动器上的独特位置 以下是我正在测试的代码,用于复制模板、添加字段以及重命名和保存:Google apps script 谷歌应用程序脚本复制和重命名文件夹结构,包括更新文件中的字段和文件夹中包含的工作表?,google-apps-script,google-drive-api,Google Apps Script,Google Drive Api,我必须首先道歉,因为我以前从未做过任何编码。我一直在用谷歌搜索和查看其他人的代码,试图找出到底发生了什么。我有逻辑,但我不知道所有的类、触发器,甚至不知道正确的术语!我可以在脑海中勾勒出需要发生什么,但首先需要学习语言,所以希望你能为我指出学习材料的正确方向,并帮助我完成这个特定的项目 我想自动化我们办公室经常做的事情,即创建一个包含预定义子文件夹的新客户文件夹,获取一个模板文档和工作表,填写名称和地址字段,并将其与客户名称一起保存到新复制/创建的相关文件夹中。我正在努力连贯地解释这一点,所以我
function autoFillGoogleDocFromForm(e) {
var timestamp = e.values[0];
var title = e.values[1];
var firstName = e.values[2];
var lastName = e.values[3];
var emailAddress = e.values[4];
var premisesType = e.values[6];
//file is the template file, and you get it by ID
var file = DriveApp.getFileById('1QjYas1erxtQjLPduNMoY1EIEAjBOI5qbAMeX59qPib4');
//We can make a copy of the template, name it, and optionally tell it what folder to live in
//file.makeCopy will return a Google Drive file object
var folder = DriveApp.getFolderById('18GWERZFqgf9TbPfKDRX8gK02kbKpRLch')
var copy = file.makeCopy(lastName + ', ' + firstName, folder);
//Once we've got the new file created, we need to open it as a document by using its ID
var doc = DocumentApp.openById(copy.getId());
//Since everything we need to change is in the body, we need to get that
var body = doc.getBody();
var header = doc.getHeader();
//Then we call all of our replaceText methods
header.replaceText('{{Premises type}}', premisesType);
body.replaceText('{{First Name}}', firstName);
body.replaceText('{{Surname}}', lastName);
body.replaceText('{{Salutation}}', title);
body.replaceText('{{email}}', emailAddress);
body.replaceText('{{Premises type}}', premisesType);
//Lastly we save and close the document to persist our changes
doc.saveAndClose(); }
// REPLICATE ABOVE WITH SHEETS, fills out cell G2 with test info
function autoFillGoogleSheetFromForm(e) {
var timestamp = e.values[0];
var title = e.values[1];
var firstName = e.values[2];
var lastName = e.values[3];
var emailAddress = e.values[4];
var premisesType = e.values[6];
//file is the template file, and you get it by ID
var file = DriveApp.getFileById('1yvOtPcnGMQnE7Hc0d6vVMSG2Q5nY9u-MijaDZ51jlOU');
//We can make a copy of the template, name it, and optionally tell it what folder to live in
//file.makeCopy will return a Google Drive file object
var folder = DriveApp.getFolderById('18GWERZFqgf9TbPfKDRX8gK02kbKpRLch')
var copy = file.makeCopy(lastName + ', ' + firstName, folder);
//Once we've got the new file created, we need to open it as a document by using its ID
var ss = SpreadsheetApp.openById(copy.getId());
// ss is now the spreadsheet the script is associated with
var sheet = ss.getSheets()[0];
// sheets are counted starting from 0
// sheet is the first worksheet in the spreadsheet
var cell = sheet.getRange("G2");
cell.setValue(premisesType);
}
希望有人能帮助我,而不会激怒我。我发布这个消息已经够尴尬的了!我保证我会努力学习,而不是简单的选择!
文斯
编辑:多亏了@iansedano,我已经编写了一些代码,这些代码让我得到了我需要的东西,除了在某些情况下,我多次创建了文件夹结构器,好像smae表单已经被重新提交了好几次。下面是我正在测试的代码:
function autoCreateFoldersSheetDoc(e) {
var title = e.values[1];
var firstName = e.values[2];
var lastName = e.values[3];
var emailAddress = e.values[4];
var premisesType = e.values[6];
// This is the root directory where all the client folders would be stored
const customerRoot = DriveApp.getFolderById('1s4fD0wk8Mj_YiCkEOj7hUZedOv_AyPdl');
const mainFolder = customerRoot.createFolder(lastName + ', ' + firstName);
// Creating the sub folders
// Some are assigned to variables so that later children can be added
mainFolder.createFolder("1. Customer Correspondence")
const costsFolder = mainFolder.createFolder("2. Costs")
const proposalsFolder = mainFolder.createFolder("3. Proposals")
mainFolder.createFolder("4. Drawings")
mainFolder.createFolder("5. Testing & Commissioning")
// Creating children
costsFolder.createFolder("1. Suppliers")
const dcaFolder = costsFolder.createFolder("2. DCA")
// Take the template proposal doc and save a copy to the proposals folder
var fileA = DriveApp.getFileById('1QjYas1erxtQjLPduNMoY1EIEAjBOI5qbAMeX59qPib4');
var copyA = fileA.makeCopy(lastName + ', ' + firstName, proposalsFolder);
// Open new proposal doc and replace text
var doc = DocumentApp.openById(copyA.getId());
var body = doc.getBody();
var header = doc.getHeader();
header.replaceText('{{Premises type}}', premisesType);
body.replaceText('{{First Name}}', firstName);
body.replaceText('{{Surname}}', lastName);
body.replaceText('{{Salutation}}', title);
body.replaceText('{{email}}', emailAddress);
body.replaceText('{{Premises type}}', premisesType);
doc.saveAndClose();
// Take the template costs sheet and save a copy to the DCA folder
var fileB = DriveApp.getFileById('1yvOtPcnGMQnE7Hc0d6vVMSG2Q5nY9u-MijaDZ51jlOU');
var copyB = fileB.makeCopy(lastName + ', ' + firstName, dcaFolder);
// Open new costs sheet and replace text
var ss = SpreadsheetApp.openById(copyB.getId());
var sheet = ss.getSheets()[0];
var cellA = sheet.getRange("G2");
var cellB = sheet.getRange("H5");
var cellC = sheet.getRange("H6");
var cellD = sheet.getRange("H4");
var cellE = sheet.getRange("I2");
cellA.setValue(premisesType);
cellB.setValue(firstName);
cellC.setValue(lastName);
cellD.setValue(title);
cellE.setValue(emailAddress);
}
您知道如何防止重复吗?示例函数
/**
*此函数接受三个参数,客户端名称,
*成本计算表id和提案文档id。
*然后,它将创建文件夹并将文档放入
*在正确的地方,无论他们在哪里。
*/
函数createClientAccountFolder(clientName、costingSheetId、ProposalDocId){
//这是存储所有客户端文件夹的根目录
const customerRoot=DriveApp.getFolderById(“[YOUR_ROOT_FOLDER_ID]”);
if(客户端名称){
//这将创建带有客户名称的主文件夹
const mainFolder=customerRoot.createFolder(clientName);
//创建子文件夹
//有些被分配给变量,以便以后可以添加子变量
mainFolder.createFolder(“1.客户通信”)
const costsFolder=mainFolder.createFolder(“2.Costs”)
const proposalsFolder=mainFolder.createFolder(“3.proposalsFolder”)
mainFolder.createFolder(“4.图纸”)
mainFolder.createFolder(“4.测试和调试”)
//创造孩子
costsFolder.createFolder(“1.供应商”)
const dcaFolder=costsFolder.createFolder(“2.DCA”)
//获取文件
const costingSheet=DriveApp.getFileById(costingSheetId)
常量proposalDoc=DriveApp.getFileById(ProposalDocId)
//将文档移动到相应的文件夹
成本表。移动到(DCA文件夹)
proposalDoc.moveTo(ProposalFolder)
}否则{
Logger.log('未指定客户端名称')
}
}
功能测试(){
createClientAccountFolder(“John Smith”,“工作表ID”,“文档ID]”)
}
我不确定如何将它集成到您的其他脚本中,因为我没有能够测试它的数据。不过,作为建议,您可以在函数autoFillGoogleDocFromForm
和autoFillGoogleSheetFromForm
的末尾添加一行
return id
您需要首先获取文档的id,并将其分配给id
变量。如果要在不做任何更改的情况下使用上述函数,请确保将其指定为字符串
因此,您需要做的就是调用main
函数中的所有函数,如下所示:
函数main(){
const clientName=“约翰·史密斯”
const docId=autoFillGoogleDocFromForm()
const sheetId=autoFillGoogleSheetFromForm()
createClientAccountFolder(clientName、sheetId、docId)
}
您可能还需要一种以不同方式获取clientName
的方法,这样可以更容易地实现自动化。希望你能对需要做的事情有所了解,并根据自己的需要进行修改
如果有任何不清楚的地方,请告诉我
工具书类