久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

將 excel 文件轉換為 Google 電子表格并自動替換現

Convert excel files to Google Spreadsheet and replace existing spreadsheet files automatically(將 excel 文件轉換為 Google 電子表格并自動替換現有的電子表格文件)
本文介紹了將 excel 文件轉換為 Google 電子表格并自動替換現有的電子表格文件的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

提供的代碼將文件從 Excel 轉換為 Google 表格.此處的代碼但它不會覆蓋/替換目標文件夾中當前現有的電子表格文件.是否可以完全轉換包括子文件夾內的所有內容并替換任何現有的同名 Google 電子表格文件?

The code provided convert files from Excel to Google Sheet. The code from here but it does not overwrite/replace current existing spreadsheet files in the destination folder. Is it possible to convert everything including the ones inside the subfolders altogether and replace any existing Google Spreadsheet files with the same name?

function convertCollection1() 
{
  var user = Session.getActiveUser(); // Used for ownership testing.1aJcbdGhwliTs_CZ-3ZUvQmGRDzBM7fv9
  var origin = DriveApp.getFolderById("1dPsDfoqMQLCokZK4RN0C0VRzaRATr9AN");
  var dest = DriveApp.getFolderById("1M6lDfc_xEkR4w61pUOG4P5AXmSGF1hGy");

  // 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;

    Logger.log(JSON.stringify(gsNames))
  }

  // 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().replace(/.[^/.]+$/, "")])
      {
        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;
      }
    }
  }
  Logger.log(JSON.stringify(gsNames))
}

推薦答案

  1. 包含多個子文件夾的文件夾中有 Excel 文件(文件擴展名為 .xlsx 或 .xls).
  2. 沒有子文件夾的文件夾中有電子表格文件(文件名不帶 .xlsx 或 .xls 的擴展名).
  3. 您想用從 Excel 文件轉換的電子表格覆蓋現有的電子表格文件.
  4. 電子表格和 Excel 文件的數量相同.

根據您的問題和評論,我可以理解為上述內容.

From your question and comments, I could understand like above.

起初,我測試了通過批處理請求更新文件.結果,當使用文件blob進行更新時,似乎無法通過批處理請求來實現文件的更新.關于這一點,如果我找到了解決這種情況的方法,我想更新我的答案.

At first, I tested to update files by the batch request. As the result, it seems that the update of file cannot be achieved by the batch request, when the file blob is used for updating. About this, if I found the workaround for this situation, I would like to update my answer.

所以在這個示例腳本中,我針對上述情況提出了使用高級 Google 服務的 Drive API 的方法.

So in this sample script, I propose the method for using Drive API of Advanced Google Services for above situation.

使用此腳本時,請在高級 Google 服務和 API 控制臺啟用 Drive API.您可以在此處查看此內容.

When you use this script, please enable Drive API at Advanced Google Services and API console. You can see about this at here.

這個腳本的流程如下.

  1. 檢索源文件夾和目標文件夾中的文件.
  2. 當源文件夾中的文件名存在于目標文件夾中時,這些文件會覆蓋現有的電子表格文件.
  3. 當源文件夾中的文件名不存在于目標文件夾中時,這些文件將作為新文件轉換為電子表格.

示例腳本:

在運行腳本之前,請設置sourceFolderIddestinationFolderId.

function myFunction() {
  var sourceFolderId = "###"; // Folder ID including source files.
  var destinationFolderId = "###"; // Folder ID that the converted files are put.

  var getFileIds = function (folder, fileList, q) {
    var files = folder.searchFiles(q);
    while (files.hasNext()) {
      var f = files.next();
      fileList.push({id: f.getId(), fileName: f.getName().split(".")[0].trim()});
    }
    var folders = folder.getFolders();
    while (folders.hasNext()) getFileIds(folders.next(), fileList, q);
    return fileList;
  };
  var sourceFiles = getFileIds(DriveApp.getFolderById(sourceFolderId), [], "mimeType='" + MimeType.MICROSOFT_EXCEL + "' or mimeType='" + MimeType.MICROSOFT_EXCEL_LEGACY + "'");
  var destinationFiles = getFileIds(DriveApp.getFolderById(destinationFolderId), [], "mimeType='" + MimeType.GOOGLE_SHEETS + "'");
  var createFiles = sourceFiles.filter(function(e) {return destinationFiles.every(function(f) {return f.fileName !== e.fileName});});
  var updateFiles = sourceFiles.reduce(function(ar, e) {
    var dst = destinationFiles.filter(function(f) {return f.fileName === e.fileName});
    if (dst.length > 0) {
      e.to = dst[0].id;
      ar.push(e);
    }
    return ar;
  }, []);
  if (createFiles.length > 0) createFiles.forEach(function(e) {Drive.Files.insert({mimeType: MimeType.GOOGLE_SHEETS, parents: [{id: destinationFolderId}], title: e.fileName}, DriveApp.getFileById(e.id))});
  if (updateFiles.length > 0) updateFiles.forEach(function(e) {Drive.Files.update({}, e.to, DriveApp.getFileById(e.id))});
}

注意:

  • 當你有很多文件要轉換,腳本執行時間結束時,請分割文件并運行腳本.
    • 高級 Google 服務
    • Drive API

    這篇關于將 excel 文件轉換為 Google 電子表格并自動替換現有的電子表格文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

    【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

discord.js v12: How do I await for messages in a DM channel?(discord.js v12:我如何等待 DM 頻道中的消息?)
how to make my bot mention the person who gave that bot command(如何讓我的機器人提及發出該機器人命令的人)
How to fix Must use import to load ES Module discord.js(如何修復必須使用導入來加載 ES 模塊 discord.js)
How to list all members from a specific server?(如何列出來自特定服務器的所有成員?)
Discord bot: Fix ‘FFMPEG not found’(Discord bot:修復“找不到 FFMPEG)
Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服務器時的歡迎消息)
主站蜘蛛池模板: 国产精品欧美精品 | 亚洲欧美激情精品一区二区 | www国产亚洲精品久久网站 | 精品久久99 | 91精品国产综合久久婷婷香蕉 | 国产精品视频播放 | 日本黄色大片免费看 | 国产在线精品一区 | 亚洲第一天堂 | 亚洲欧美在线观看 | 国产精品国色综合久久 | 91美女在线观看 | 久久亚洲精品久久国产一区二区 | 成人精品免费 | 亚洲天堂久久 | jvid精品资源在线观看 | 国产精品美女在线观看 | 欧美精品第一区 | 欧美亚洲一区二区三区 | 亚洲 欧美 另类 综合 偷拍 | 国产女人第一次做爰毛片 | 精品国产一区二区三区性色av | 精品久久视频 | 日本视频免费观看 | 久久精品欧美一区二区三区不卡 | 国产成人精品福利 | 黄色在线 | 91视频一区二区三区 | 欧美激情a∨在线视频播放 成人免费共享视频 | 成年人的视频免费观看 | 日韩一级电影免费观看 | 一级毛片在线播放 | 免费在线看黄视频 | 超碰3 | 色婷婷综合久久久中字幕精品久久 | 一区二区三区免费 | 成年人黄色一级毛片 | 精品一区二区三区不卡 | 成人免费区一区二区三区 | 在线观看国产视频 | 成人自拍视频 |