【问题标题】:How to format js strings to have a Google Sheet/Excel line break in a single cell?如何格式化 js 字符串以在单个单元格中使用 Google Sheet/Excel 换行符?
【发布时间】:2021-08-07 23:06:31
【问题描述】:

背景

我正在尝试使用纯 JavaScript 格式化一些字符串。它将被复制粘贴到 Google 表格中的单个单元格中,中间有一个换行符(alt + enter)。例如,

Sample Image

代码

let result = 'info1'
result += ', '         // replace with a line break
result += 'info2'

CopyToClipboard(result)

问题

我最接近的尝试是使用 Google Sheet equation 和 char(10)

let result = '="info1"&char(10)&"info2"'

但由于此解决方案更改了实际数据,因此从最终用户的角度来看,很难进行编辑和重用。我认为必须有一个更简单的方法,例如 /t/n

我不太成功的尝试包括:

String.fromCharCode(10)

/r //r /n //n /t //t

任何建议将不胜感激!

【问题讨论】:

    标签: javascript excel google-sheets google-chrome-extension


    【解决方案1】:

    使用反斜杠\,但不使用斜杠/

    \n

    var result;
    
    result = 'info1'
    result += '\n'
    result += 'info2'
    /* OR */
    result = 'info1\ninfo2'
    

    替代:Template literals

    let result = `info1
    info2`;
    

    【讨论】:

    • 不幸的是,两者都不起作用。 ;_;工作表忽略 '\n' 字符并将第二个字符解释为一堆空格。不过还是谢谢你!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-02
    • 1970-01-01
    • 2020-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多