iT邦幫忙

2024 iThome 鐵人賽

DAY 12
0
IT 管理

30 天玩轉 GAS: 打造你的個人自動化助手系列 第 12

[Day 12] GAS - Google Sheet 操作大全 Part 5 - RichTextValue 讓文字不只是文字

  • 分享至 

  • xImage
  •  

相信很多時候你在使用 google sheet 插入文字的時候, 有時候會使用到粗體斜體、設定字體顏色、超連結等等。
我們知道如何在 google sheet 上手動操作,但在 GAS 上要怎麼實作呢
這時候我們需要用到 RichTextValue 這個 Class!

字型

讓我們一個一個步驟分解來看吧,如果我們今天想要一個
"Hello, GAS!" 的文字, Hello 想要粗體, GAS! 想要藍色

1. setText 設定純文字

設定純文字,需要在

  // 創建 RichTextValueBuilder, 並設下文字
  var richText = SpreadsheetApp.newRichTextValue()
                               .setText("Hello, GAS!");

2. newTextStyle 新增字型

  var textStyleBold = SpreadsheetApp.newTextStyle().setBold(true).build();
  var textStyleBlue = SpreadsheetApp.newTextStyle().setForegroundColor("#0000FF").build(); // 藍色

3. setTextStyle 設定字型

  richText.setTextStyle(0, 5, textStyleBold); // "Hello" 位置為 0-5
  richText.setTextStyle(7, 10, textStyleBlue); // "GAS!" 位置為 7-10

4. 建立RichText並應用至儲存格

  // 將 RichTextValue 應用到 A1 儲存格
  sheet.getRange('A1').setRichTextValue(richText.build());

完成程式碼

function setRichTextInCell() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  
  // 創建 RichTextValueBuilder, 並設下文字
  var richText = SpreadsheetApp.newRichTextValue()
                               .setText("Hello, GAS!");

  // 設置 'Hello,' 為粗體
  var textStyleBold = SpreadsheetApp.newTextStyle().setBold(true).build();
  richText.setTextStyle(0, 5, textStyleBold); // "Hello" 位置為 0-5
  
  // 設置 'World!' 為藍色
  var textStyleBlue = SpreadsheetApp.newTextStyle().setForegroundColor("#0000FF").build();
  richText.setTextStyle(7, 10, textStyleBlue); // "World!" 位置為 7-10
  
  // 將 RichTextValue 應用到 A1 儲存格
  sheet.getRange('A1').setRichTextValue(richText.build());
}

也可以將上述 RichTextValue 的一系列操作,簡化成一行:

 var richText = SpreadsheetApp.newRichTextValue()
    .setText("Hello, GAS!")
    .setTextStyle(0, 5, textStyleBold)
    .setTextStyle(7, 10, textStyleBlue)
    .build();

Result

https://ithelp.ithome.com.tw/upload/images/20240925/20137680ayeuaRlm6B.png

超連結

我們還還能用 RichTextValue 來在Google Sheets的儲存格中建立超連結
甚至只可針對部分文字作超連結,也可在同一個儲存格內建立多個超連結,接下來就看下面範例吧!

設定整個值的連結 setLinkUrl

var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const value = SpreadsheetApp.newRichTextValue()
    .setText("Click here to visit Google website")
    .setLinkUrl("https://www.google.com/");
// 將 RichTextValue 應用到 A1 儲存格
sheet.getRange('A1').setRichTextValue(value.build());

Result

https://ithelp.ithome.com.tw/upload/images/20240925/20137680SVERCdR4dI.png

指定子字串連結網址 setLinkUrl(startOffset, endOffset, textStyle)


const value = SpreadsheetApp.newRichTextValue()
    .setText("google & yahoo")
    .setLinkUrl(0, 6, "https://google.com")
    .setLinkUrl(9, 14, "https://yahoo.tw");

Result

https://ithelp.ithome.com.tw/upload/images/20240925/20137680U1OwUu4bvk.png

如過有多個 RichTextValue 要套用到多個儲存格,可使用 setRichTextValues 的用法,詳細可參見官網
恭喜你如何學會使用 RichTextValue 啦~ 下一天就正式進入 Google Slide 的世界啦!


上一篇
[Day 11] GAS - Google Sheet 操作大全 Part 4 - 工作表操作
下一篇
[Day 13] GAS - Google Slide 操作大全 - Part 1 簡報 Presentation &投影片 Slide
系列文
30 天玩轉 GAS: 打造你的個人自動化助手13
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言