自动将表格保存在文件夹中

Automatically save form in folder

我在 Google Apps 脚本中编写了一个脚本,用于从 Google 表格创建新表单。 如何将表格自动保存到驱动器的特定文件夹中?

你不说,但我们假设你正在创建一个 Google 表单,例如像这样:

var form=FormApp.create('My form');

在这种情况下,这里有一个函数可以将该表单的驱动器文件添加到您指定的驱动器文件夹中。注意 - 此函数可以支持文档、电子表格和其他文件类型...任何具有 getId() 方法并且也可以表示为云端硬盘文件的 Google Apps 脚本对象。

/**
 * Places file for given item into given folder.
 * If the item is an object that does not support the getId() method or
 * the folder is not a Folder object, an error will be thrown.
 * From: 
 *
 * @param {Object}  item     Any object that has an ID and is also a Drive File.
 * @param {Folder}  folder   Google Drive Folder object.
 */
function saveItemInFolder(item,folder) {
  var id = item.getId();  // Will throw error if getId() not supported.
  folder.addFile(DriveApp.getFileById(id));
}

示例:

// Create temporary form 
var form=FormApp.create('Delete me');
// Get temporary folder
var folder=DriveApp.getFoldersByName('Delete me').next();

saveItemInFolder(form,folder);

或者如果您有 ID,您可以使用 DriveApp 实际移动表单

Like so, but you'd do it only for 1 file (your form ID)