google-apps-script - 如何在 toast() 中显示换行符/CR?

标签 google-apps-script google-sheets

我试图在 toast() 中包含换行符消息,但这似乎不起作用:

 SpreadsheetApp.getActiveSpreadsheet().toast("Actions effectués: " + chargement + "\n Durée moyenne:\n - 10 + 32 par groupes simples \n - 42 + (64/groupes) pour les groupes composés.", "Chargement en cours:\n", 2);

有没有办法做到这一点,或替代方案? (因为我怀疑这是可能的,这并不是说我的代码很难理解所以我可能会犯错误。)

最佳答案

msgBox()inputBox() ,您可以通过转义 \n 上的斜杠来发布多行消息:

Browser.msgBox("line 1\\nline 2\\nline 3");

同样的技巧不适用于 toast() , 很遗憾。

另一个老技巧是用空格或不间断空格填充消息的行,以强制后面的文本到下一行。这很有效,但很麻烦。

所以这里有一个实用程序可以轻松做到这一点! (this gist 的一部分。)您不想这样做吗?

function testToaster() {
  var myToast = new Toaster( "Actions effectués: " + chargement + "\n Durée moyenne:\n - 10 + 32 par groupes simples \n - 42 + (64/groupes) pour les groupes composés.", "Chargement en cours:\n", 2);
  myToast.display();
}

如果您知道每个字符的确切宽度加上任何内部填充,以及显示区域的确切宽度,您就可以完美地填充每个字符串。这个实用程序不是那么精确,但非常接近。

/**
 * "Class" Toaster
 *
 * From http://stackoverflow.com/a/33552904/1677912
 *
 * Wrapper for Spreadsheet.toast() with support for multi-line messages.
 *
 * Constructor:    new Toaster( message, title, timeoutSeconds );
 *
 * @param message         {String}    Toast message, possibly with newlines (`\n`)
 * @param title           {String}    (optional) Toast title
 * @param timeoutSeconds  {Number}    (optional) Duration of display, default 3s
 *
 * @returns {Toaster}                 Toaster instance.
 */
var Toaster = function(message, title, timeoutSeconds) {
  if (typeof message == 'undefined')
    throw new TypeError( "missing message" );

  this.message = this.parseMessage(message);
  this.title = title || '';
  this.timeoutSeconds = timeoutSeconds || 3;
  this.ss = SpreadsheetApp.getActiveSpreadsheet();
};

/**
 * Display Toaster message using previously set parameters.
 */
Toaster.prototype.display = function() {
  this.ss.toast(this.message,this.title,this.timeoutSeconds);
}

/**
 * This is where the magic happens. Prepares multi-line messages for display.
 *
 * @param {String} msg    Toast message, possibly with newlines (`\n`)
 *
 * @returns{String}       Message, ready to display.
 */
Toaster.prototype.parseMessage = function( msg ) {
  var maxWidth = 52;             // Approx. number of non-breaking spaces required to span toast popup.
  var knob = 1.85;               // Magical approx. ratio of avg char width : non-breaking space width
  var parsedMessage = '';

  var lines = msg.split('\n');   // Break lines at newline chars

  // Rebuild message with padded lines
  for (var i=0; i<lines.length; i++) {
    var len = lines[i].length;
    // Build padding string of non-breaking spaces sandwiched with normal spaces.
    var padding = ' '
                + len < (maxWidth / knob) ?
                  Array(Math.floor(maxWidth-(lines[i].length * knob))).join(String.fromCharCode(160)) + ' ' : '';
    parsedMessage += lines[i] + padding;
  }
  return parsedMessage;
}

关于google-apps-script - 如何在 toast() 中显示换行符/CR?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33549379/

相关文章:

html - 脚本已完成,但返回的值不是受支持的返回类型?

javascript - 运行 Google 协作平台小部件的权限问题

javascript - 从范围列表中返回值,而无需事后展平

google-apps-script - 脚本 'setValues' 方法将字符串解释为单元格中的数字

google-apps-script - Google Apps 脚本中国防部网站的 SSL 验证

javascript - Google Apps脚本-如何登录和获取数据?

google-apps-script - 具有关联服务器的 Google Workspace 插件 : managing OAuth and permissions

google-sheets - 谷歌电子表格查询下拉列表

javascript - 如何查询谷歌工作表列的某个值

google-sheets - 我正在尝试使用 vlookup 作为条件格式公式