[英]Script to automatically make a copy of a Google Document for editing
我觉得在这里发帖完全是菜鸟。 我很了解 CSS、HTML 和 XML,但一直避开 JS。 我对 javascript 知之甚少,最近开始学习 Lynda.com 课程以赶上进度。 对不起我的无知。 因此,我真的很难学习 Google Apps 脚本。 显然,我需要先学习 JS,然后才能理解它。
我工作的学校(5000 名学生)开设了在线课程。 我以数千个谷歌文档工作表的形式创建了课程。 这些工作表链接在各种网站上。
我们面临的问题是,当学生打开文档时,他们必须复制一份才能编辑它们(我当然不希望他们能够编辑原件)。 对于在平板电脑上使用移动浏览器的学生来说,这真的很糟糕,因为在移动设备上使用桌面 UI 时,在 Google 文档中制作副本效果不佳。
我知道这种事情可以用脚本自动化。 我看过这里,低看,它有效。 三年来我一直在寻找这样的功能,我高兴得快要尿裤子了,(是的。我知道这很可悲)。
所以,我要问的是,是否有人愿意帮助新手弄清楚如何调整此代码,以便学生在网站课程上单击一个按钮,它会自动制作并在新选项卡中打开工作表的副本?
/**
* Copy an existing file.
*
* @param {String} originFileId ID of the origin file to copy.
* @param {String} copyTitle Title of the copy.
*/
function copyFile(originFileId, copyTitle) {
var body = {'title': copyTitle};
var request = gapi.client.drive.files.copy({
'fileId': originFileId,
'resource': body
});
request.execute(function(resp) {
console.log('Copy ID: ' + resp.id);
});
}
昨天花了一整天的时间学习Javascript,我到go还有很长的路要走。不知道我要花多长时间才能自己弄清楚。
您当然可以使用 Apps 脚本来做到这一点。 只需要几行。 事实上,你可以只使用我在下面写的版本。
这是我会怎么做 -
确保您的原始文档至少可供访问它的人阅读。
从 URL 中获取 fileId -
使用以下代码在 Apps 脚本中编写一个网络应用程序 -
function doGet(e) { //file has to be at least readable by the person running the script var fileId = e.parameters.fileId; if(!fileId){ //have a default fileId for testing. fileId = '1K7OA1lnzphJRuJ7ZjCfLu83MSwOXoEKWY6BuqYitTQQ'; } var newUrl = DocsList.getFileById(fileId).makeCopy('File copied to my drive').getUrl(); return HtmlService.createHtmlOutput('<h1><a href="'+newUrl+'">Open Document</a></h1>'); }
部署它以作为访问应用程序的人运行。
要记住的一件关键事情是,由 Apps Script 构建的 Web 应用程序无法自动强制打开新窗口。 相反,我们可以显示一个链接,该链接可在编辑模式下点击进入文档。
您可以在此处看到它的运行情况(将创建一些虚拟文件)-
您可以通过放入自己的fileId
来测试。
由于DocsList
已被弃用,目前您可以使用以下代码制作文件的副本:
File file=DriveApp.getFileById(fileId).makeCopy(fileName, folder);
可以按照 Arun Nagarajan 的回答中的说明获取fileId
。
截至 2015 年更新, Google Script出于未知原因删除了 fileId 。 之前将“/copy”附加到 Google 文档的 URL 的方法已重新启用。 例如) https://docs.google.com/document/d/1GTGuLqahAKS3ptjrfLSYCjKz4FBecv4dITPuKfdnrmY/复制
这是正确执行此操作的代码(适用于 2019、2020、2021):
/**
* Create custom menu when document is opened.
*/
function onOpen() {
DocumentApp.getUi()
.createMenu('For Students')
.addItem('Make a copy', 'makeACopy')
.addToUi();
}
function makeACopy() {
var templateId = DocumentApp.getActiveDocument().getId();
DriveApp.getFileById(templateId).makeCopy();
}
这是我用来自动复制我的谷歌文档的代码。
function makeCopy() {
// generates the timestamp and stores in variable formattedDate as year-month-date hour-minute-second
var formattedDate = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd' 'HH:mm:ss");
// gets the name of the original file and appends the word "copy" followed by the timestamp stored in formattedDate
var name = DocumentApp.getActiveDocument().getName() + " Copy " + formattedDate;
// gets the destination folder by their ID. REPLACE xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx with your folder's ID that you can get by opening the folder in Google Drive and checking the URL in the browser's address bar
var destination = DriveApp.getFolderById("16S8Gp4NiPaEqZ0Xzz6q_qhAbl_thcDBF");
// gets the current Google Docs file
var file = DriveApp.getFileById(DocumentApp.getActiveDocument().getId())
添加触发器并享受!
相同的代码适用于谷歌表格。 只有你需要更换
DocumentApp.getActiveDocument().getName()
和
SpreadsheetApp.getActiveSpreadsheet()
您可以在此处找到更多详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.