我还可以使用其他什么方法

我还可以使用其他什么方法

本文介绍了.addFile似乎已被弃用,我还可以使用其他什么方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将文件从Google云端硬盘复制到我将其设置为的目标位置,但是.addFile似乎已被弃用.我尝试使用moveTomakeCopy,但是我似乎无法使其正常工作.这是我现在拥有的代码:

I want to make a copy of a file from Google Drive to the destination I set it to but .addFile seems to be deprecated.I tried using moveTo and makeCopy but i cant seem to get it to work. This is the code i have for now:

function copyfile() {
var ss = SpreadsheetApp.getActive();
var activeSheet = ss.getSheetByName("sheet_Patients");
var activeSheetFM = ss.getSheetByName("_FolderMaker_");
var lastRow = activeSheet.getLastRow();
var data = activeSheet.getRange(lastRow,3,1,1).getValue();
var secondData = activeSheet.getRange(lastRow,4,1,1).getValue();
var folder_Name = [data+ ", " +secondData];

var folders = DriveApp.getFoldersByName(folder_Name);
while (folders.hasNext()) {
      const folder = folders.next();
      var id = folder.getId();
}
activeSheetFM.getRange(2,4,1,1).setValue(id);

var file = DriveApp.getFileById("1g25_24OTv_t5Qav2Q1hwEM_YBiPSaWWb");
var source_folder = DriveApp.getFolderById("1pqh74miSSy9WDSD3kpi02kkI6XDuXiuU");
var dest_folder = DriveApp.getFoldersByName(id);
//Logger.log(finalId);


var finalFile = file.makeCopy(data+ ', '+ secondData + ' ' + Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'yyyy-MM-dd') + ' ' + file.getName());
dest_folder.addFile(finalFile);
source_folder.removeFile(finalFile);
}

推荐答案

修改点:

  • 在您的脚本中,var dest_folder = DriveApp.getFoldersByName(id);中的dest_folder是FolderIterator.
    • 我认为这可能是您有关I tried using moveTo and makeCopy but i cant seem to get it to work.的问题的原因.
    • Modification points:

      • In your script, dest_folder of var dest_folder = DriveApp.getFoldersByName(id); is FolderIterator.
        • I thought that this might be the reason of your issue about I tried using moveTo and makeCopy but i cant seem to get it to work..
        • 当以上几点反映到您的脚本中时,它如下所示.

          When above points are reflected to your script, it becomes as follows.

          var folders = DriveApp.getFoldersByName(folder_Name);
          while (folders.hasNext()) {
                const folder = folders.next();
                var id = folder.getId();
          }
          activeSheetFM.getRange(2,4,1,1).setValue(id);
          
          var file = DriveApp.getFileById("1g25_24OTv_t5Qav2Q1hwEM_YBiPSaWWb");
          var source_folder = DriveApp.getFolderById("1pqh74miSSy9WDSD3kpi02kkI6XDuXiuU");
          var dest_folder = DriveApp.getFoldersByName(id);
          //Logger.log(finalId);
          
          
          var finalFile = file.makeCopy(data+ ', '+ secondData + ' ' + Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'yyyy-MM-dd') + ' ' + file.getName());
          dest_folder.addFile(finalFile);
          source_folder.removeFile(finalFile);
          

          到:

          var folders = DriveApp.getFoldersByName(folder_Name);
          var dest_folder;
          if (folders.hasNext()) {
            dest_folder = folders.next();
          } else {
            throw new Error("No folder.")
          }
          activeSheetFM.getRange(2,4,1,1).setValue(dest_folder.getId());
          var file = DriveApp.getFileById("1g25_24OTv_t5Qav2Q1hwEM_YBiPSaWWb");
          var finalFile = file.makeCopy(data+ ', '+ secondData + ' ' + Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'yyyy-MM-dd') + ' ' + file.getName());
          finalFile.moveTo(dest_folder);
          

          参考文献:

          • getFoldersByName(name)
          • moveTo(目标)
          • References:

            • getFoldersByName(name)
            • moveTo(destination)
            • 这篇关于.addFile似乎已被弃用,我还可以使用其他什么方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:20