TKScript icon indicating copy to clipboard operation
TKScript copied to clipboard

复制腾讯文档表格会现格式错误

Open 7hens opened this issue 5 months ago • 2 comments

问题描述

腾讯文档复制 excel 表格会出现格式错误。

如针对以下表格:

H1 H2
A
B
C

复制之后的的文本为:

H1 H2\nA\nB C

将该文本复制到 excel 表格后,会呈现下面的样式:

H1 H2
A
B C

项目所属

  • [x] Copy
  • [ ] Force Copy
  • [ ] Copy Currency
  • [ ] Site Director
  • [ ] Captcha
  • [ ] Expansion
  • [ ] Completion
  • [ ] Unknown

相关链接

https://docs.qq.com/sheet/DR1p2a2lqTWRRSklE?tab=BB08J2

预期表现

见问题描述。

解决方案

相关代码:

https://github.com/WindrunnerMax/TKScript/blob/master/packages/copy/src/modules/docqq.ts

          if (selection) {
            const text = [];
            const { startColIndex, startRowIndex, endColIndex, endRowIndex } = selection;
            for (let i = startRowIndex; i <= endRowIndex; i++) {
              for (let k = startColIndex; k <= endColIndex; k++) {
                const cell = SpreadsheetApp.workbook.activeSheet.getCellDataAtPosition(i, k);
                if (!cell)
                  continue;
                text.push(" ", ((_a = cell.formattedValue) == null ? void 0 : _a.value) || cell.value || "");
              }
              i !== endRowIndex && text.push("\n");
            }
            const str = text.join("");
            return /^\s*$/.test(str) ? "" : str;
          }

问题代码:

text.push(" ", ((_a = cell.formattedValue) == null ? void 0 : _a.value) || cell.value || "");

修改后的代码:

text.push("\t", (((_a = cell.formattedValue) == null ? void 0 : _a.value) || cell.value || "").replace(/\n/g, '\\n'));

经过上面的修改,得到的结果为:

H1 H2
A\nB C

遗留问题:单元格内的换行被转换成了 \\n

7hens avatar Sep 24 '24 14:09 7hens