从 Google 表单创建新文档并添加到新文件夹

Create new document from Google form and add to a new folder

我有一个 Google 脚本在提交表单时触发。它基于模板创建一个新文档,其中根据表单中的答案填充了某些变量。

我在表单提交时也创建了一个文件夹。

我遇到的问题是在新创建的文件夹中创建新创建的文档。寻求一些帮助来合并两个独立运行的脚本以实现此目的。

正在提交表单时创建文件夹:

function createChannelFolder() {
var ss = SpreadsheetApp.getActive();
var names = ss.getSheetByName("Completed Certifications");
var ChannelName = names.getRange(names.getLastRow(), 2).getValue(); 
var parentFolder=DriveApp.getFolderById("FOLDERID");
return newFolder=parentFolder.createFolder(ChannelName);
  
}

正在提交表单时创建文档

function autoFillGoogleDocFromForm(e) {
  //e.values is an array of form values
  var Timestamp = e.values[0];
  var Channel = e.values[1];
  var Name = e.values[2];;
  
  var file = DriveApp.getFileById('FILEID'); 
  
  var folder = DriveApp.getFolderById('FOLDERID')
  var copy = file.makeCopy(Channel + ',' + Name, folder); 
  var newId = copy.getId();
  var doc = DocumentApp.openById(newId); 

  var body = doc.getBody(); 
  body.replaceText('{{Timestamp}}', Timestamp); 
  body.replaceText('{{Channel}}', Channel);  
  body.replaceText('{{Name}}', Name);
         
  doc.saveAndClose(); 
}

解释:

  • 里面需要调用createChannelFolder() autoFillGoogleDocFromForm(e).

  • 只是 return createChannelFolder():

    中的文件夹对象

    return parentFolder.createFolder(ChannelName);


解决方案:

这里是autoFillGoogleDocFromForm(e):

function autoFillGoogleDocFromForm(e) {
  //e.values is an array of form values
  var Timestamp = e.values[0];
  var Channel = e.values[1];
  var Name = e.values[2];;
  
  var file = DriveApp.getFileById('FILEID'); 
  
  var folder = createChannelFolder(); // 1st modification point
  var copy = file.makeCopy(Channel + ',' + Name, folder); 
  var newId = copy.getId();
  var doc = DocumentApp.openById(newId); 

  var body = doc.getBody(); 
  body.replaceText('{{Timestamp}}', Timestamp); 
  body.replaceText('{{Channel}}', Channel);  
  body.replaceText('{{Name}}', Name);
         
  doc.saveAndClose(); 
}

这里是 createChannelFolder():

function createChannelFolder() {
var ss = SpreadsheetApp.getActive();
var names = ss.getSheetByName("Completed Certifications");
var ChannelName = names.getRange(names.getLastRow(), 2).getValue(); 
var parentFolder=DriveApp.getFolderById("FOLDERID");
return parentFolder.createFolder(ChannelName); // 2nd modification point
  
}