簡體   English   中英

谷歌表格,兩種字體大小一個單元格

[英]Google Sheets, Two Font Sizes One Cell

我有一個包含數百行數據的電子表格,來自數據驗證列表。

有些數據是高度相關的,因此我希望它更大,有些數據對大多數人來說有用但多余。

不同的字體大小都可以。 如果有人是專家,那么不同的顏色和格式會很棒

我需要腳本是動態的,即 | 左側的數據。 以一種方式格式化,位於 | 的右側用另一種方式。

在此處輸入圖像描述

我相信你的目標如下。

  • 您想使用 Google Apps 腳本(來自您的google-sheets-macros標記)修改單元格中部分文本的文本樣式。
  • 您想修改NAME NAME | CODECODE | NAME NAME | CODE
    • 您不想修改NAME NAME + 的文字樣式 + | + .
    • 您只想修改CODE的文本樣式。
    • Different font size is fine. If anyone is an expert with though, different colours and format would be amazing Different font size is fine. If anyone is an expert with though, different colours and format would be amazing ,你想修改文本的字體大小和前景色。 在這種情況下,您期望的format是粗體和斜體?
  • 您想保留NAME NAMENAME NAME | CODE NAME NAME | CODE

我認為使用 RichTextValue 可以實現上述目標。 在這個答案中,我想提出示例腳本。

樣品情況:

此示例腳本假設NAME NAME | CODE的值的示例情況 NAME NAME | CODE放在“A”列中。 所以為了測試它,請准備好這種情況。

示例腳本:

請將以下腳本復制並粘貼到電子表格的容器綁定腳本中,並設置工作表名稱。

function myFunction() {
  const sheetName = "Sheet1";  // Please set the sheet name.
  
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName(sheetName);
  const range = sheet.getRange("A1:A" + sheet.getLastRow());
  const richTextValues = range.getRichTextValues().map(([a]) => {
    const text = a.getText();
    const pos = text.indexOf("|");
    if (pos > -1) {
      const temp = a.getTextStyle(0, pos - 1);
      const textStyle = SpreadsheetApp.newTextStyle()
        .setFontSize(5)
        .setForegroundColor("red")
        .setItalic(true)
        .setBold(true)
        .build();
      return [
        SpreadsheetApp.newRichTextValue()
        .setText(text)
        .setTextStyle(0, pos - 1, temp)
        .setTextStyle(pos + 2, text.length, textStyle)
        .build()
      ];
    }
    return [SpreadsheetApp.newRichTextValue().setText(text).setTextStyle(a.getTextStyle()).build()];
  });
  range.setRichTextValues(richTextValues);
}

結果:

當您運行上述腳本時,“A”列的文本 styles 被重新格式化並修改了CODE的文本樣式,而NAME NAME | CODENAME NAME的文本樣式 NAME NAME | CODE未修改。 而且,沒有|的單元格的文本樣式未修改。 您可以在下圖中看到示例結果。

在此處輸入圖像描述

筆記:

  • 此示例腳本是一個簡單的示例。 所以請根據您的實際情況進行修改。

參考:

添加:

當你想轉換選定的范圍時,下面的示例腳本怎么樣? 使用此腳本時,首先請在 select 范圍內運行此腳本。 通過這種方式,所選范圍被轉換。

function myFunction() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const select = ss.getSelection();
  const sheet = select.getActiveSheet();
  const range = select.getActiveRange();
  const richTextValues = range.getRichTextValues().map(([a]) => {
    const text = a.getText();
    const pos = text.indexOf("|");
    if (pos > -1) {
      const temp = a.getTextStyle(0, pos - 1);
      const textStyle = SpreadsheetApp.newTextStyle()
        .setFontSize(5)
        .setForegroundColor("red")
        .setItalic(true)
        .setBold(true)
        .build();
      return [
        SpreadsheetApp.newRichTextValue()
        .setText(text)
        .setTextStyle(0, pos - 1, temp)
        .setTextStyle(pos + 2, text.length, textStyle)
        .build()
      ];
    }
    return [SpreadsheetApp.newRichTextValue().setText(text).setTextStyle(a.getTextStyle()).build()];
  });
  range.setRichTextValues(richTextValues);
}
  • 如果要將轉換后的值放在列的右側,請將上面的腳本修改如下。

    •  range.setRichTextValues(richTextValues);
    •  range.offset(0, 1).setRichTextValues(richTextValues);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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