[英]Preserve Formatting When Setting New Cell Value (Google Spreadsheet)
我有以下腳本為選定的電子表格單元格設置新值。 它按預期工作,但每次輸入后它都會清除單元格中的所有先前格式。 如何修改代碼以確保它保留所有部分格式,例如字體粗細/顏色? 感謝您的幫助。
function enterName1(options1, activity1, number1) {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getActiveSheet();
var cell = sheet.getActiveCell();
var value = cell.getValue();
var formattedDate = Utilities.formatDate(new Date(), "GMT+3", "dd.MM.yy' at 'HH:mm");
Logger.log(formattedDate);
if (value === '') {
value = value + "📞 " + formattedDate + " call " + options1 + number1 + ": " + activity1;
sheet.getActiveRange().setNumberFormat('@STRING@');
cell.setValue(value);
} else {
value = value + "\n\n" + "📞 " + formattedDate + " call " + options1 + number1 + ": " + activity1;
sheet.getActiveRange().setNumberFormat('@STRING@');
cell.setValue(value);
}
}
我相信你的目標如下。
formattedDate
設置文本樣式。 - 問題 3:當將值添加到包含值的單元格時,您希望保留單元格中文本的現有文本樣式。 而且,您希望為添加文本的formattedDate
設置文本樣式。 而且,即使單元格為空,您也希望為添加文本的formattedDate
設置文本樣式。當你的腳本被修改時,它變成如下。
} else { value = value + "\\n\\n" + "📞 " + formattedDate + " call " + options1 + number1 + ": " + activity1; sheet.getActiveRange().setNumberFormat('@STRING@'); cell.setValue(value); }
到:
} else { value = value + "\\n\\n" + "📞 " + formattedDate + " call " + options1 + number1 + ": " + activity1; var richTextValue = cell.getRichTextValue(); var existingStyles = richTextValue.getRuns().map(e => ({start: e.getStartIndex(), end: e.getEndIndex(), style: e.getTextStyle()})); var richTexts = SpreadsheetApp.newRichTextValue().setText(value); existingStyles.forEach(e => richTexts.setTextStyle(e.start, e.end, e.style)); cell.setRichTextValue(richTexts.build()); cell.setNumberFormat('@STRING@'); }
formattedDate
的文本樣式,然后在添加值后設置文本樣式。 “RichTextValue”也用於此。當你的腳本被修改時,它變成如下。
} else { value = value + "\\n\\n" + "📞 " + formattedDate + " call " + options1 + number1 + ": " + activity1; sheet.getActiveRange().setNumberFormat('@STRING@'); cell.setValue(value); }
到:
var textStyle = SpreadsheetApp.newTextStyle().setBold(true).setForegroundColor("#FF0000").build(); // Please set this for the additional text.
if (value === '') {
value = "📞 " + formattedDate + " call " + options1 + number1 + ": " + activity1;
var richTexts = SpreadsheetApp
.newRichTextValue()
.setText("📞 " + formattedDate + " call " + options1 + number1 + ": " + activity1)
.setTextStyle(("📞 ").length, ("📞 " + formattedDate).length, textStyle);
cell.setRichTextValue(richTexts.build());
cell.setNumberFormat('@STRING@');
} else {
var richTextValue = cell.getRichTextValue();
var existingStyles = richTextValue.getRuns().map(e => ({start: e.getStartIndex(), end: e.getEndIndex(), style: e.getTextStyle()}));
var startOffset = (value + "\n\n" + "📞 ").length;
existingStyles.push({start: startOffset, end: startOffset + formattedDate.length, style: textStyle});
var richTexts = SpreadsheetApp.newRichTextValue().setText(value + "\n\n" + "到 " + formattedDate + " call " + options1 + number1 + ": " + activity1);
existingStyles.forEach(e => richTexts.setTextStyle(e.start, e.end, e.style));
cell.setRichTextValue(richTexts.build());
cell.setNumberFormat('@STRING@');
}
formattedDate
。formattedDate
具有粗體和紅色字體顏色。formattedDate
的文本樣式,然后在添加值后設置文本樣式。 “RichTextValue”也用於此。 此外,當單元格為空時,需要為添加文本的formattedDate
設置文本樣式。當你的腳本被修改時,它變成如下。
if (value === '') { value = value + "📞 " + formattedDate + " call " + options1 + number1 + ": " + activity1; sheet.getActiveRange().setNumberFormat('@STRING@'); cell.setValue(value); } else { value = value + "\\n\\n" + "📞 " + formattedDate + " call " + options1 + number1 + ": " + activity1; sheet.getActiveRange().setNumberFormat('@STRING@'); cell.setValue(value); }
到:
var textStyle = SpreadsheetApp.newTextStyle().setBold(true).setForegroundColor("#FF0000").build(); // Please set this for the additional text. if (value === '') { value = "📞 " + formattedDate + " call " + options1 + number1 + ": " + activity1; var richTexts = SpreadsheetApp .newRichTextValue() .setText("📞 " + formattedDate + " call " + options1 + number1 + ": " + activity1) .setTextStyle(("📞 ").length, ("📞 " + formattedDate).length, textStyle); cell.setRichTextValue(richTexts.build()); cell.setNumberFormat('@STRING@'); } else { var richTextValue = cell.getRichTextValue(); var existingStyles = richTextValue.getRuns().map(e => ({start: e.getStartIndex(), end: e.getEndIndex(), style: e.getTextStyle()})); var startOffset = (value + "\\n\\n" + "📞 ").length; existingStyles.push({start: startOffset, end: startOffset + formattedDate.length, style: textStyle}); var richTexts = SpreadsheetApp.newRichTextValue().setText(value + "\\n\\n" + "到 " + formattedDate + " call " + options1 + number1 + ": " + activity1); existingStyles.forEach(e => richTexts.setTextStyle(e.start, e.end, e.style)); cell.setRichTextValue(richTexts.build()); cell.setNumberFormat('@STRING@'); }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.