Google Apps脚本会创建表格版本的excel文件。多次创建版本时出现问题。

时间:2018-11-21 23:41:21

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

我在另一则帖子Google Apps Script creates sheets version of excel file中找到了针对原始问题的解决方案。

使用答案中提供的代码进行测试,我遇到了另一个问题。每次运行脚本时,即使它们已经存在,它也会再次创建.xlsx文件的Spreadsheets版本。我尝试用最后的If修改代码,但没有结果。然后回去运行您发布的代码,以防万一我错过了一些东西,但是它会继续创建相同文件的版本。

任何能解决该问题的想法将不胜感激。

答案提供的代码如下。

// Convert the user's stored excel files to google spreadsheets based on the specified directories.
// There are quota limits on the maximum conversions per day: consumer @gmail = 250.
function convertExcelToGoogleSheets() 
{
  var user = Session.getActiveUser(); // Used for ownership testing.
  var origin = DriveApp.getFolderById("origin folder id");
  var dest = DriveApp.getFolderById("destination folder id");

  // Index the filenames of owned Google Sheets files as object keys (which are hashed).
  // This avoids needing to search and do multiple string comparisons.
  // It takes around 100-200 ms per iteration to advance the iterator, check if the file
  // should be cached, and insert the key-value pair. Depending on the magnitude of
  // the task, this may need to be done separately, and loaded from a storage device instead.
  // Note that there are quota limits on queries per second - 1000 per 100 sec:
  // If the sequence is too large and the loop too fast, Utilities.sleep() usage will be needed.
  var gsi = dest.getFilesByType(MimeType.GOOGLE_SHEETS), gsNames = {};
  while (gsi.hasNext())
  {
    var file = gsi.next();
    if(file.getOwner().getEmail() == user.getEmail())
      gsNames[file.getName()] = true;
  }

  // Find and convert any unconverted .xls, .xlsx files in the given directories.
  var exceltypes = [MimeType.MICROSOFT_EXCEL, MimeType.MICROSOFT_EXCEL_LEGACY];
  for(var mt = 0; mt < exceltypes.length; ++mt)
  {
    var efi = origin.getFilesByType(exceltypes[mt]);
    while (efi.hasNext())
    {
      var file = efi.next();
      // Perform conversions only for owned files that don't have owned gs equivalents.
      // If an excel file does not have gs file with the same name, gsNames[ ... ] will be undefined, and !undefined -> true
      // If an excel file does have a gs file with the same name, gsNames[ ... ] will be true, and !true -> false
      if(file.getOwner().getEmail() == user.getEmail() && !gsNames[file.getName()])
      {
        Drive.Files.insert(
          {title: file.getName(), parents: [{"id": dest.getId()}]},
          file.getBlob(),
          {convert: true}
        );
        // Do not convert any more spreadsheets with this same name.
        gsNames[file.getName()] = true;
      }
    }
  }
}

1 个答案:

答案 0 :(得分:0)

  • 您要将origin文件夹中的Excel文件转换为Google Spreadsheet,然后将转换后的Spreadsheet放置到dest文件夹中。
  • 当转换后的文件的文件名存在于dest文件夹中时,您不想转换它。

如果我的理解是正确的,那么该修改如何?

发件人:

if(file.getOwner().getEmail() == user.getEmail() && !gsNames[file.getName()])

收件人:

if(file.getOwner().getEmail() == user.getEmail() && !gsNames[file.getName().split(".")[0]])

注意:

  • 在此修改中,当在dest文件夹中找到已转换文件的文件名时,不会转换该文件。
  • 当文件名具有扩展名,例如###.xlsx并将其转换为Google Spreadsheet时,该扩展名似乎会自动删除。我认为这是创建重复文件的原因。因此,在这种情况下,我使用了split(".")[0]

参考: