使用 Office 脚本将 Excel 工作簿另存为单独的 CSV 文件

问题描述 投票:0回答:1

我想自动化一项消耗大量时间的日常任务。有一个包含许多工作簿的 Excel 文件。每个工作簿都需要保存为单独的 CSV 文件,以便我能够将其上传到我们的平台。

我创建了以下脚本:

function saveWorksheetsAsCSV() {
  let workbook = ExcelScript.Workbook.getActiveWorkbook();
  let worksheets = workbook.getWorksheets();

  // Get the user's downloads folder path
  let downloadsFolderPath = OfficeExtension.configurableWebSystemService.getDownloadsFolderPath();

  // Get the current date and time
  let currentDate = new Date();
  let formattedDate = currentDate.getFullYear() + "-" + padNumber(currentDate.getMonth() + 1) + "-" + padNumber(currentDate.getDate());
  let formattedTime = padNumber(currentDate.getHours()) + "-" + padNumber(currentDate.getMinutes()) + "-" + padNumber(currentDate.getSeconds());

  worksheets.forEach(function (worksheet) {
    // Create a unique filename based on the worksheet name, date, and time
    let filename = "DiscountPrograms_" + worksheet.getName() + "_" + formattedDate + "_" + formattedTime + ".csv";
    let filePath = downloadsFolderPath + "\\" + filename;

    // Save the worksheet as CSV with semicolon delimiter
    worksheet.saveAs(filePath, ExcelScript.SaveAsFileType.CommaSeparatedValue, { delimiter: ";" });
  });
}

function padNumber(number) {
  return number.toString().padStart(2, '0');
}

但是,当我运行它时,我只是收到以下错误:

[13, 32] Parameter 'worksheet' implicitly has an 'any' type, but a better type may be inferred from usage.
[23, 20] Parameter 'number' implicitly has an 'any' type, but a better type may be inferred from usage.
[2, 30] Property 'Workbook' does not exist on type 'typeof ExcelScript'.
[6, 29] Cannot find name 'OfficeExtension'.
[19, 44] Property 'SaveAsFileType' does not exist on type 'typeof ExcelScript'.
[1, 10] No function of name 'main' found. Ensure the function exists and is not nested in anything.
[2, 7] Office Scripts cannot infer the data type of this variable. Please declare a type for the variable.
[3, 7] Office Scripts cannot infer the data type of this variable. Please declare a type for the variable.
[6, 7] Office Scripts cannot infer the data type of this variable. Please declare a type for the variable.
[23, 20] Office Scripts cannot infer the data type of this variable. Please declare a type for the variable.

你能帮我解决一下吗?这是我第一次创建脚本。谢谢!

excel csv export-to-csv office-scripts
1个回答
0
投票

可能为时已晚,但请查看 Power Automate 社区中的这篇文章: https://powerusers.microsoft.com/t5/Power-Automate-Cookbook/Save-XLSX-Excel-all-worksheets-as-csv/td-p/923157

© www.soinside.com 2019 - 2024. All rights reserved.