脚本自动制作Google文档的副本进行编辑 [英] Script to automatically make a copy of a Google Document for editing

查看:268
本文介绍了脚本自动制作Google文档的副本进行编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得这里总是有一个noob的贴子。我非常了解CSS,HTML和XML,但总是避免使用JS。我知道JavaScript很少,最近开始了一个Lynda.com课程来赶上。对不起,我的无知。因此,我非常努力学习Google Apps脚本。很明显,我需要学习JS,然后才能理解它。



我为(5000名学生)工作的学校建立了在线课程。我以数千个Google文档工作表的形式创建了课程表。这些工作表在各种网站上链接。



我们面临的问题是,当学生打开文档时,他们必须在编辑它们之前制作它们的副本(我当然不希望它们能够编辑原稿)。这对于在平板电脑上使用移动浏览器的学生来说确实很糟糕,因为在移动设备上使用桌面用户界面时,在Google文档中制作副本并不会很好。

我知道这种事情可以通过脚本自动完成。我在 doGet(e){
//文件必须至少可以被运行脚本的人读取
var fileId = e.parameters.fileId;
if(!fileId){
//有一个默认的fileId用于测试。
fileId ='1K7OA1lnzphJRuJ7ZjCfLu83MSwOXoEKWY6BuqYitTQQ';
}
var newUrl = DocsList.getFileById(fileId).makeCopy('File copied to my drive')。getUrl();
return HtmlService.createHtmlOutput('< h1>< a href ='+ newUrl +'>打开文档< / a>< / h1>');
}


  • 将其部署为以访问应用程序的人身份运行。




  • 要记住的一个关键是,由Apps脚本构建的Web应用程序无法强制自动打开新窗口。相反,我们可以在编辑模式下显示可点击到文档中的链接。

    你可以在这里看到它的动作(将创建一些虚拟文件) -

    https://script.google.com/macros/s/AKfycbyvxkYqgPQEb3ICieywqWrQ2-2KWb -V0MghR2xayQyExFgVT2h3 / exec?fileId = 0AkJNj_IM2wiPdGhsNEJzZ2RtZU9NaHc4QXdvbHhSM0E 您可以通过将您自己的 fileId


    I feel like a total noob posting here. I know CSS, HTML, and XML pretty well but have always avoided JS. I know very little javascript and recently started a Lynda.com course to catch up. Sorry for my ignorance. As such, I am really struggling learning Google Apps Script. Obviously, I need to learn JS before I can make sense of any of it.

    The school I work for (5000 students) has set up an online curriculum. I created the curriculum in the form of thousands of google document worksheets. These worksheets are linked on various websites.

    The problem we are facing is that when students open the documents, they have to make a copy of them before they can edit them (I of course don't want them to be able to edit the originals). This really sucks for students using mobile browsers on their tablets as making a copy in Google Docs doesn't really work well when using the desktop UI on mobile devices.

    I know this kind of thing can be automated with script. I've looked here, and low and behold, it works! I'm pissing my pants with joy as I've been searching for such functionality for three years. (Yes, I know that's sad).

    So, what I'm asking is, would anyone be willing to help a noob figure out how to adapt this code so that students click a button on a website lesson and it automatically makes and opens a copy of the worksheet in a new tab?

    /**
     * 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);
      });
    } 
    

    Spending all day yesterday learning Javascript, I've still got a long way to go. Not sure how long it'll take for me to be able to figure this out on my own.

    解决方案

    You can certainly do this with Apps Script. Only takes a couple of lines. In fact, you can use just the version I wrote below.

    Here is how I would do it -

    1. Ensure you original document is at least read enabled for the folks that will be accessing it.

    2. Grab the fileId from the URL -

    3. Write a web app in Apps Script with the following code -

      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>');
      }
      

    4. Deploy it to run as the person accessing the app.

    One key thing to remember is that a web app built by Apps Script cannot force open a new window automatically. Instead we can show a link which is clickable into the document in edit mode.

    You can see it in action here (will create some dummy file) -

    https://script.google.com/macros/s/AKfycbyvxkYqgPQEb3ICieywqWrQ2-2KWb-V0MghR2xayQyExFgVT2h3/exec?fileId=0AkJNj_IM2wiPdGhsNEJzZ2RtZU9NaHc4QXdvbHhSM0E

    You can test this by putting in your own fileId.

    这篇关于脚本自动制作Google文档的副本进行编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆