首页 > 解决方案 > 如何在 Excel 单元格中插入换行符

问题描述

我必须解析从 Zephyr 为 Jira 导出的测试用例的 csv 文件。我需要以一种可以将它们导入新的测试管理系统的方式对它们进行格式化。新格式要求测试步骤位于一个由换行符分隔的单元格中,如下所示:

单元格格式

这是我编写的用于解析的脚本。步骤和预期结果都添加到同一个单元格中,但没有换行符。

const fs = require("fs");
const Papa = require("papaparse");

// ******************* user defined values ******************* //
// file to parse
const file = process.argv[2];

// ******************* parse csv ******************* //

const parsedTests = [];

// create file object
const fileObject = fs.readFileSync(file, "utf8");

// set config
const config = {
  header: true,
  complete: function(results) {
    return results;
  }
};

// parse and save results
const results = Papa.parse(fileObject, config);
const arr = Object.values(results.data);

function addTests(parsedTests, test) {
  const found = parsedTests.some(el => el.ExecutionId === test.ExecutionId);
  if (!found) {
    parsedTests.push(test);
  }
  return parsedTests;
}

function addSteps(parstedTests, test, el) {
  const found = parsedTests.some(el => el.ExecutionId === test.ExecutionId);
  if (found && test.OrderId > 1) {
    test.Step += `${test.Step}\n\n`;
    test.ExpectedResults += `${test.ExpectedResults}\n\n`;
  }
}

arr.forEach(el => {
  addTests(parsedTests, el);
});

arr.forEach(el => {
  addSteps(parsedTests, el);
});

console.log(parsedTests)


// write back to csv
fs.writeFileSync("./parsedtests.csv", Papa.unparse(parsedTests));

标签: javascriptexcel

解决方案


推荐阅读