Google Apps脚本:标签字符变为空格

时间:2015-08-12 22:23:59

标签: google-apps-script

我有一个字符串文字,想要显示它。但是,选项卡会转换为空格。

var s = "Row" + "\t" + "Email \n";  
ui.alert("String: ", s, ui.ButtonSet.OK);

我该如何解决这个问题?这里的答案不适用于我Tab characters converted to spaces in Google Apps Script

1 个答案:

答案 0 :(得分:1)

接口ui.alert删除空格(多个空格)。如果您在Logger.log中显示它,您将看到制表符距离。

虽然您可以在电子表格中显示一个窗口:ui.showModalDialog。 它使用了html,在html中你可以强制空格来打印& nbsp。你必须将你的\ t替换为四次& nbsp并在modalDialog中显示它。祝你好运!

function TabsInModalDialog() {
  var ui = SpreadsheetApp.getUi();
  var title = "Tabs allowed";
  var message = "Hi there!\tLet's make use of the Tab!\t\tThese were two tabs\t\t\tAnd these were three even!";
  var html = message.replace(/\t/g,"&nbsp&nbsp&nbsp&nbsp"); // this replaces the tab \t with four spaces, &nbsp is special code for a space in html.
  html = HtmlService.createHtmlOutput(html).setSandboxMode(HtmlService.SandboxMode.IFRAME);
  ui.showModalDialog(html, title);
}