簡體   English   中英

設置新單元格值時保留格式(Google 電子表格)

[英]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);
  }
}

我相信你的目標如下。

  • 問題1:當值添加到包含值的單元格時,您希望保留單元格中文本的現有文本樣式。
  • 問題2:當值添加到包含值的單元格時,您希望保留單元格中文本的現有文本樣式。 而且,您想為添加文本的formattedDate設置文本樣式。 - 問題 3:當將值添加到包含值的單元格時,您希望保留單元格中文本的現有文本樣式。 而且,您希望為添加文本的formattedDate設置文本樣式。 而且,即使單元格為空,您也希望為添加文本的formattedDate設置文本樣式。

回答問題 1:

改裝要點:

  • 在這種情況下,需要檢索現有值的文本樣式,並在添加值后設置文本樣式。 “RichTextValue”用於此目的。

當你的腳本被修改時,它變成如下。

修改后的腳本:

從:
 } 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@'); }
  • 在這個修改后的腳本中,設置了現有值的文本樣式。 所以添加的文字沒有文字樣式。 請注意這一點。

回答問題 2:

改裝要點:

  • 在這種情況下,需要檢索現有值的文本樣式並設置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具有粗體和紅色字體顏色。

回答問題 3:

改裝要點:

  • 在這種情況下,需要檢索現有值的文本樣式並設置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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM