简体   繁体   English

设置新单元格值时保留格式(Google 电子表格)

[英]Preserve Formatting When Setting New Cell Value (Google Spreadsheet)

I have the following script which sets a new value for a selected spreadsheet cell.我有以下脚本为选定的电子表格单元格设置新值。 It works as expected but after every entry it clears all the previous formatting from the cell .它按预期工作,但每次输入后它都会清除单元格中的所有先前格式 How can I modify the code to make sure it preserves all the partial formatting, such as font weight/color?如何修改代码以确保它保留所有部分格式,例如字体粗细/颜色? Thank you for your help.感谢您的帮助。

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);
  }
}

I believe your goal as follows.我相信你的目标如下。

  • Question 1: You want to preserve the existing text styles of the text in the cell when the value is added to the cell including the values.问题1:当值添加到包含值的单元格时,您希望保留单元格中文本的现有文本样式。
  • Question 2: You want to preserve the existing text styles of the text in the cell when the value is added to the cell including the values.问题2:当值添加到包含值的单元格时,您希望保留单元格中文本的现有文本样式。 And also, you want to set the text style for formattedDate of the adding text.- Question 3: You want to preserve the existing text styles of the text in the cell when the value is added to the cell including the values.而且,您想为添加文本的formattedDate设置文本样式。 - 问题 3:当将值添加到包含值的单元格时,您希望保留单元格中文本的现有文本样式。 And also, you want to set the text style for formattedDate of the adding text.而且,您希望为添加文本的formattedDate设置文本样式。 And also, even when the cell is empty, you want to set the text style for formattedDate of the adding text.而且,即使单元格为空,您也希望为添加文本的formattedDate设置文本样式。

Answer for question 1:回答问题 1:

Modification points:改装要点:

  • In this case, it is required to retrieve the text styles of the existing values and set the text styles after the value was added.在这种情况下,需要检索现有值的文本样式,并在添加值后设置文本样式。 "RichTextValue" is used for this. “RichTextValue”用于此目的。

When your script is modified, it becomes as follows.当你的脚本被修改时,它变成如下。

Modified script:修改后的脚本:

From: 从:
 } else { value = value + "\\n\\n" + "📞 " + formattedDate + " call " + options1 + number1 + ": " + activity1; sheet.getActiveRange().setNumberFormat('@STRING@'); cell.setValue(value); }
To: 到:
 } 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@'); }
  • In this modified script, the text styles of the existing value are set.在这个修改后的脚本中,设置了现有值的文本样式。 So the added text has no text style.所以添加的文字没有文字样式。 Please be careful this.请注意这一点。

Answer for question 2:回答问题 2:

Modification points:改装要点:

  • In this case, it is required to retrieve the text styles of the existing values and also set the text style for formattedDate , and then, set the text styles after the value was added.在这种情况下,需要检索现有值的文本样式并设置formattedDate的文本样式,然后在添加值后设置文本样式。 "RichTextValue" is also used for this. “RichTextValue”也用于此。

When your script is modified, it becomes as follows.当你的脚本被修改时,它变成如下。

Modified script:修改后的脚本:

From: 从:
 } else { value = value + "\\n\\n" + "📞 " + formattedDate + " call " + options1 + number1 + ": " + activity1; sheet.getActiveRange().setNumberFormat('@STRING@'); cell.setValue(value); }
To: 到:
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@');
}
  • In this modified script, the text styles of the existing value and formattedDate of the adding text are set.在这个修改后的脚本中,设置了现有值的文本样式和添加文本的formattedDate
  • As above sample, formattedDate has the bold and red font color.如上示例, formattedDate具有粗体和红色字体颜色。

Answer for question 3:回答问题 3:

Modification points:改装要点:

  • In this case, it is required to retrieve the text styles of the existing values and also set the text style for formattedDate , and then, set the text styles after the value was added.在这种情况下,需要检索现有值的文本样式并设置formattedDate的文本样式,然后在添加值后设置文本样式。 "RichTextValue" is also used for this. “RichTextValue”也用于此。 And also, when the cell is empty, it is required to set the text style for formattedDate of the adding text.此外,当单元格为空时,需要为添加文本的formattedDate设置文本样式。

When your script is modified, it becomes as follows.当你的脚本被修改时,它变成如下。

Modified script:修改后的脚本:

From: 从:
 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); }
To: 到:
 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@'); }

Reference:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM