复制文件后获取文档的URL/ID [英] Get document's URL/ID after a file was copied

查看:60
本文介绍了复制文件后获取文档的URL/ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在复制完成后更新新复制的文档,将其添加到其他文件夹中,并删除其根文件.Google脚本无法找到新复制的文件.错误出现在代码的下半部分,并带有带TypeError的星号.

I am trying to have the newly copied document to be updated upon completion of being copied, added to a different folder and its root file removed. The Google script is not able to find the newly copied file. The error is in the lower half of the code surrounded by asterisks with TypeError.

我怀疑这与 var doc = DocumentApp.getActiveDocument(); 这一行有关.我不确定如何获取新复制的文档的URL( file2 Date-Weekly Agenda ).

I suspect it has to do with the line of var doc = DocumentApp.getActiveDocument();. I am not sure how to get the URL of the newly copied document (file2 or Date - Weekly Agenda).

  function copyupdateWeeklyAgenda() {
  var file = DriveApp.getFileById("FILEKEY");
  var source_folder = DriveApp.getFolderById("FOLDERKEY");
  var dest_folder = DriveApp.getFolderById("FOLDERKEY");
  // Make a backup copy.
  var file2 = file.makeCopy('Date - Weekly Agenda');
  dest_folder.addFile(file2);
  source_folder.removeFile(file2);

//START Update Weekly Agenda Data ---
  var ssUrl = 'https://whydoyouwanttoknow.com';
  var sheetName = 'Sheet1';   // name of sheet to use
  var rangeName = 'A1:B2';    // range of values to include 
  var values = SpreadsheetApp.openByUrl(ssUrl)
                             .getSheetByName(sheetName)
                             .getRange(rangeName)
                             .getValues();
  var doc = DocumentApp.getActiveDocument();
  **var body = doc.getBody();** <--- TYPEERROR: Cannot call method "getBody" of null.
  var ranges = doc.getNamedRanges('embeddedSheet-range');
  if (ranges.length == 0) {
    var table = body.appendTable(values);
  ...ETC.

推荐答案

您的 file2 变量是类 File 的对象,因为这就是

Your file2 variable is an object of class File, because this is what File#makeCopy returns. To get the corresponding Document, use

var doc = DocumentApp.openById(file2.getId());

备注

  1. 方法 getActiveDocument 引用脚本所附加到的文档(如果有).这并不意味着脚本当前正在处理的事情".

  1. The method getActiveDocument refers to the Document to which the script is attached (if any). It does not mean "the thing that the script is currently handling".

如果要处理电子表格,则 var ss = SpreadsheetApp.open(file2)将返回 file2 中包含的 Spreadsheet .代码>.显然,API设计人员忘记了在 DocumentApp 中包含 open 方法,因此我们必须如上所述检查文件ID.

If you were dealing with a spreadsheet, then var ss = SpreadsheetApp.open(file2) would return the Spreadsheet contained in file2. Apparently, the API designers forgot to include the open method in DocumentApp, so we have to go through the file ID as above.

这篇关于复制文件后获取文档的URL/ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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