【问题标题】:Google Apps Script : Making an A Function Execute on OpenGoogle Apps 脚本:在打开时执行 A 函数
【发布时间】:2014-02-03 20:36:51
【问题描述】:

我找到了一个脚本,可以将我的电子表格移动到一个文件夹中。我怎样才能使它在电子表格打开时运行函数。我将把它应用到模板上,这样每次打开模板时,电子表格文件的“副本”都会自动移动到我的装箱单文件夹中。

这是我的代码......

function MoveFileTo()  
{

    var docs=DocsList.find('Save Test'); 
    var doc=docs[0];   /*Since we assume there is only one file with the name "Hello World",  we take the index 0 */
    var folders = doc.getParents(); 
    var newFolder=DocsList.getFolder('Packing Lists');
    doc.addToFolder(newFolder); // This will add the document to its new location.
    var docParentFolder=folders[0]; 
    doc.removeFromFolder(docParentFolder); // This will remove the document from its original location. 
}

编辑:我尝试将函数 MoveFileTo 更改为 onOpen()onEdit(),但它似乎无法正常工作。有什么我遗漏的吗?

【问题讨论】:

标签: google-apps-script google-sheets google-drive-api


【解决方案1】:

尝试使用 driveapp 而不是 doclist(已弃用)。 impinball评论真的很有用,你应该看看它。
无论如何这里有一个工作代码:

function myFunction() {
  var destFolder = DriveApp.getFolderById("FOLDER_ID_WHERE8YOU_WANT_TO_MOVE_YOUR_FILES");
  var fileList = DriveApp.searchFiles("mimeType = 'application/vnd.google-apps.spreadsheet' and title contains 'FILE_TITLE'"); // change this search if necessary
  var originalFileId="THE_ID_OF_THE_ACTUAL_SPREADSHEET_TO_BE_EXCLUDED_FROM_THE_SCRIPT_TREATMENT";
  while(fileList.hasNext()){
    var fileToMove = fileList.next();
    if(fileToMove.getId()==originalFileId){
      continue; // don't do anything if it's the original file
    }
    var papas = fileToMove.getParents();
    destFolder.addFile(fileToMove); // do the job
    while(papas.hasNext()){
      papas.next().removeFile(fileToMove); // remove the actuals parents (can be more than one)
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-10
    • 1970-01-01
    • 1970-01-01
    • 2020-11-25
    相关资源
    最近更新 更多