首页 > google-apps-script > 如何使用Google Apps脚本将多个Google文档添加到Google云端硬盘文件夹?

如何使用Google Apps脚本将多个Google文档添加到Google云端硬盘文件夹? (How can I use a Google Apps Script to add multiple Google Docs to a Google Drive folder?)

问题

我有一个Google文档网址列表(大约700个),需要将它们添加到Google文件夹中,以便它们都在一个空间中,因为目前它们由数百个不同的用户拥有。

我已经看过一些关于如何执行此操作的帖子:从文件夹的内容生成URL列表。这有帮助,但似乎这项任务要困难得多。

这是我尝试过的,它似乎没有起作用:

var doc = DocumentApp.openById('1sQGds0kyeO66ZiMiJlsKkHPcqjySPy5q0dWShc2irts'),
docFile = DriveApp.getFileById( doc.getId() );
DriveApp.getFolderById('0B7yp85g7j5ZHbVlEUmNGX2w0M1k').addFile( docFile );
DriveApp.getRootFolder().removeFile(docFile);

解决方法

它没有错误检查,可能会超时...但它是一个开始。您将在A列中添加fileIds。

function makeCopyFromId() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;  // First row of data to process
  var numRows = 700;   // Last row to process.
  var dataRange = sheet.getRange(startRow, 1, numRows, 5)
  var data = dataRange.getValues();
  var destFolder = DriveApp.getFolderById("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); 
  for (i in data) {
    var row = data[i];
    var ssId = row[0];  
    Logger.log(ssId);   // check the log to see if they all complete-
  
     var destSpreadsheet = SpreadsheetApp.open(DriveApp.getFileById(ssId).makeCopy("",destFolder))  //copies each file.     
  }}


问题

I have a list of Google Document URLs (about 700 of them) and need to add them to a Google Folder so that they are all in one space because currently they are owned by hundreds of different users.

I've seen a few postings on how to do the inverse of this: Generate a list of URLs from the contents of a folder. This has been helpful, but it seems this task is much more difficult.

This is what I've tried, and it doesn't seem to be working:

var doc = DocumentApp.openById('1sQGds0kyeO66ZiMiJlsKkHPcqjySPy5q0dWShc2irts'),
docFile = DriveApp.getFileById( doc.getId() );
DriveApp.getFolderById('0B7yp85g7j5ZHbVlEUmNGX2w0M1k').addFile( docFile );
DriveApp.getRootFolder().removeFile(docFile);

解决方法

it doesn't error check and may timeout... but it is a start. You would add the fileIds in Column A.

function makeCopyFromId() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;  // First row of data to process
  var numRows = 700;   // Last row to process.
  var dataRange = sheet.getRange(startRow, 1, numRows, 5)
  var data = dataRange.getValues();
  var destFolder = DriveApp.getFolderById("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); 
  for (i in data) {
    var row = data[i];
    var ssId = row[0];  
    Logger.log(ssId);   // check the log to see if they all complete-
  
     var destSpreadsheet = SpreadsheetApp.open(DriveApp.getFileById(ssId).makeCopy("",destFolder))  //copies each file.     
  }}

相似信息