使用Google App脚本将XLSX文件从GDRIVE导入到电子表格

发布于 2025-02-02 22:54:17 字数 104 浏览 3 评论 0原文

我目前正在创建一个脚本,该脚本可以导入从GDRive文件夹中存储的XLSX文件到电子表格,我可能会知道是否有人可以探索我的参考或示例,我已经在我的目的进行了一些研究,但是其中一些在CSV流程中。

I'm currently creating a script that can import xlsx file stored from gdrive folder to spreadsheet, may I know if someone have reference or sample that I can explore I've already do some research on my end but some of them are in CSV process.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风启觞 2025-02-09 22:54:17

首先:这不是赠品服务。没有代码样本和具体问题,我们无济于事。

但是我有这个躺在周围让您入门。启用高级驱动服务。诀窍是首先转换为表格,然后获取数据。

function getExcelData() {
  //The excel file id -> Open with spreadsheetApp and copy the id from url
  const excel = DriveApp.getFileById("excelfileif");
  //ID of the folder where the tempfile can be placed.
  const tempFolder = "tempFolderId"
  const tempFile = Drive.Files.insert(
          {title: "TempFile", parents: [{"id": tempFolder}]},
          excel.getBlob(),
          {convert: true}
        );
  const tempID = tempFile.getId();
  const source = SpreadsheetApp.openById(tempID);
  //The sheetname of the excel where you want the data from
  const sourceSheet = source.getSheetByName("sheetname");
  //The range you want the data from
  const sourceValues = sourceSheet.getRange("A1:G20").getValues();
  
  const target = SpreadsheetApp.getActiveSpreadsheet();
  //The targetsheetname
  const targetSheet = target.getSheetByName("TargetSheetName");
  
  targetSheet.getRange(targetSheet.getLastRow()+1, 1, sourceValues.length, sourceValues[0].length).setValues(sourceValues);
  
  DriveApp.getFileById(tempID).setTrashed(true);
  
}

Firstly: this not some giveaway service. Without a code sample and specific question we cannot help out.

But i have this laying around to get you started. Enable advanced drive services. The trick is to first convert to sheets and then get the data.

function getExcelData() {
  //The excel file id -> Open with spreadsheetApp and copy the id from url
  const excel = DriveApp.getFileById("excelfileif");
  //ID of the folder where the tempfile can be placed.
  const tempFolder = "tempFolderId"
  const tempFile = Drive.Files.insert(
          {title: "TempFile", parents: [{"id": tempFolder}]},
          excel.getBlob(),
          {convert: true}
        );
  const tempID = tempFile.getId();
  const source = SpreadsheetApp.openById(tempID);
  //The sheetname of the excel where you want the data from
  const sourceSheet = source.getSheetByName("sheetname");
  //The range you want the data from
  const sourceValues = sourceSheet.getRange("A1:G20").getValues();
  
  const target = SpreadsheetApp.getActiveSpreadsheet();
  //The targetsheetname
  const targetSheet = target.getSheetByName("TargetSheetName");
  
  targetSheet.getRange(targetSheet.getLastRow()+1, 1, sourceValues.length, sourceValues[0].length).setValues(sourceValues);
  
  DriveApp.getFileById(tempID).setTrashed(true);
  
}
提笔书几行 2025-02-09 22:54:17

您可以尝试使用此脚本,其中Excel文件被转换为斑点,然后使用Google电子表格格式将斑点保存为另一个文件:

function convert() {
  var excelFileName = "Test File.xlsx"; //File name of the file to be converted
  var files = DriveApp.getFilesByName(excelFileName);
  var excelFile = (files.hasNext()) ? files.next() : null;
  var blob = excelFile.getBlob();
  var config = {
    title: "[Converted File] " + excelFile.getName(), //sets the title of the converted file
    parents: [{id: excelFile.getParents().next().getId()}],
    mimeType: MimeType.GOOGLE_SHEETS
  };
  var spreadsheet = Drive.Files.insert(config, blob);
  console.log(spreadsheet.id); //Displays the file ID
}

请注意,您还需要在Google Apps脚本的“服务”部分中启用Drive API。另外,我用于此测试案例的文件还保存在我的Google驱动器的主要目录中,而不是放置在任何文件夹中。因此,输出文件也应保存在主要的Google Drive目录中。

有关更多信息,您可以访问此网站:

You can try this script wherein the excel file is converted into a blob and then saved the blob as another file with a google spreadsheet format:

function convert() {
  var excelFileName = "Test File.xlsx"; //File name of the file to be converted
  var files = DriveApp.getFilesByName(excelFileName);
  var excelFile = (files.hasNext()) ? files.next() : null;
  var blob = excelFile.getBlob();
  var config = {
    title: "[Converted File] " + excelFile.getName(), //sets the title of the converted file
    parents: [{id: excelFile.getParents().next().getId()}],
    mimeType: MimeType.GOOGLE_SHEETS
  };
  var spreadsheet = Drive.Files.insert(config, blob);
  console.log(spreadsheet.id); //Displays the file ID
}

Please take note that you also need to enable the Drive API in the services section of Google Apps Script. Also, the file that I used for this test case was saved in the main directory of my google drive and not placed within any folder. Hence, the output file should also be saved in the main google drive directory as well.

For more information, you may visit this site:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文