【问题标题】:app script, after file.makeCopy create a new file, how to access this new fileapp脚本,file.makeCopy创建一个新文件后,如何访问这个新文件
【发布时间】:2020-12-11 08:28:32
【问题描述】:

我正在尝试将附件从 Gmail 获取到 Google 云端硬盘。在文件夹中创建一个新文件后,我在访问脚本创建的新文件时遇到了问题。

与 makeCopy 函数相同

错误:异常:文档不可访问。请稍后再试。在 myFunction(代码:21:37)

感谢任何帮助和建议。谢谢!

function myFunction() {
  const files = DriveApp.getFolderById("xxxxxxxxxxxxxxxxxxx").getFiles();
  while (files.hasNext()){
    var file = files.next();
    var filename = file.getName();
    const aname = "xxxx.docx"
    if (filename == aname){
      var oldid = file.getId();
      var copyfile = file.makeCopy().setName("COPYYYYY"); 
      var newid = copyfile.getId();
    }
  }
  var body = DocumentApp.openById(newid).getBody();
  Logger.log(oldid);
  Logger.log(newid);
  Logger.log(body);
}

【问题讨论】:

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


    【解决方案1】:

    修改点:

    • 在您的脚本中,我认为当var copyfile = file.makeCopy().setName("COPYYYYY");file 是Google 文档时,该脚本有效。但是从您的错误消息和const aname = "xxxx.docx" 来看,我认为您可能会尝试使用DocumentApp.openById 直接打开DOCX 文件。如果是这样,你的问题的原因是这样的。
    • 为了使用DocumentApp.openById打开DOCX文件,在当前阶段,需要将DOCX文件转换为谷歌文档。

    当以上几点反映到你的脚本中时,它变成如下。

    修改后的脚本:

    在使用此脚本之前,please enable Drive API at Advanced Google services

    function myFunction() {
      const files = DriveApp.getFolderById("xxxxxxxxxxxxxxxxxxx").getFiles();
      var newid = "";
      while (files.hasNext()){
        var file = files.next();
        var filename = file.getName();
        const aname = "xxxx.docx";
        if (filename == aname){
          var oldid = file.getId();
          if (file.getMimeType() == MimeType.MICROSOFT_WORD) {
            var copyfile = Drive.Files.copy({title: "COPYYYYY"}, oldid, {convert: true});
            newid = copyfile.id;
          }
        }
      }
      if (newid) {
        var body = DocumentApp.openById(newid).getBody();
        Logger.log(oldid);
        Logger.log(newid);
        Logger.log(body);
      }
    }
    
    • 在这个修改后的脚本中,DOCX 文件是通过 Drive API 的“Files: copy”方法转换的。

    参考:

    【讨论】:

    • 我想我找到了答案,google正在禁止这个功能。stackoverflow.com/questions/32077888/…
    • @JFtyv_85StvsDpDn 感谢您的回复。我为我糟糕的英语水平道歉。很遗憾,我无法理解您的回复。可以问一下你的具体情况吗?由此,我想确认您的问题。
    猜你喜欢
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    • 2016-04-01
    • 2014-12-10
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多