我试图在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()
不起作用。
另一个老技巧是用空格或不间断空格填充邮件的行,迫使后面的文字出现在下一行。这很有效,但很麻烦。
因此,这里有一个实用程序可以做到这一点,而不需要麻烦!(这是要点的一部分。)你不想这样做吗?
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;
}
我遇到了同样的问题,但我发现\r似乎有效。尝试:SpreadsheetApp.getActiveSpreadsheet().toast("Actions effectués: " + chargement + "r Durée moyenne:r - 10 + 32 par groupes simples r - 42 + (64/groupes) pour les groupes composés.", "Chargement en cours:r", 2);