來練習幫 Google 文件增加一點內容吧。
今日要點:
》Document Service 文件服務範例
》新增標題與表格建立
前一天我們簡單介紹了 Document Service 的手冊與 Google 文件內容的對應,今天繼續來一些 API 程式使用範例。我們系列主要是讀取文件中表格的內容來做資料轉換,但表格的內容主要是用人工去建立調整。不過未來也可許可以自動建立文件。
所以今天先來介紹與練習一下新增標題與表格建立的相關 API,來用 Apps Script 來建立一下我們前面說明的鐵人賽 Demo 表,我試著把程式碼弄的很簡單,把一些參數先寫死比較好說明。
程式碼與執行結果對照如下圖:
如果跟要原本的文件比較,會發現有一點點不一樣,目前 Apps Script 要合併欄位可以用 merge() 但他沒有 colspan 的效果,所以今天產生的表格範例先沒有處理合併欄位。是可以用其他方法來達成合併欄位的效果,但有點複雜就先不介紹了,可以查看一下下方參考的連結。
為了方便我們就不建一個新的文件檔,讀取原有的檔案,如果想建立新檔案再加入內容,只要把 DocumentApp.openById 的地方改用 create 就好了。
我們程式大概分幾個部份
程式碼如下,裡面用到的自定 function 我放在下方的程式附錄裡,可以參考看看:
function createDemoApiDocs_SetNewContent() {
  //讀取文件檔並清空內容
  var doc = DocumentApp.openById('1LqMPLrOFN14Tn5fc0N942Lt4PJqlgnp2pK-rUSHEzTk');  
  var body = doc.getBody();
  body.clear();
  //加上標題
  appendTitle(body, "鐵人賽 API 文件 Demo");
  //加上3行空白
  appendEmptyline(body,3);
  //加上表格及內容
  table = appendTable(body);
  //設定表格欄位內容的樣式
  setTableCellStyle(table);
}
以上就是建立 Google 文件內容,新增標題與表格建立的範例,今天就先這樣囉。
function appendTitle(body, text){
  var title = body.getParagraphs()[0];
  title.insertText(0,text);
  title.setHeading(DocumentApp.ParagraphHeading.TITLE);
  title.setAlignment(DocumentApp.HorizontalAlignment.CENTER);
} 
function appendEmptyline(body, lines){
  for (var i=0; i<lines; i++){
    var emptyline = body.appendParagraph("");
    emptyline.setHeading(DocumentApp.ParagraphHeading.NORMAL);
    emptyline.setAlignment(DocumentApp.HorizontalAlignment.LEFT);
  }
} 
我們表格內容先固定內容。
function appendTable(body){
  table = body.appendTable([
    ["Docs Demo","getGmailInfo","查詢Gmail資訊"],
    ["說明"," 使用 Google Apps Script 查詢 Gmail 資訊。",""],
    [" Request Url Params ","",""],["鍵值","型別","說明"],
    [" name"," String","查詢人的姓名"],
    [" func (必填)"," String","要呼叫的功能名稱"],
    [" Response Body","",""],["鍵值","型別","說明"],
    [" hello"," String","對查詢人的問候語"],
    [" unreadCount"," Number","收件夾中未讀信件的數量"],
    [" spamCount"," Number","垃圾信件夾中的未讀數量"],
    [" messageSubject"," ArrayObject","最新5筆信件的標題"],
    [" Sample","",""],
    ["Method","GET",""],
    ["URL","https://script.google.com/...",""],
    ["Request","?name=Jason&func=getGmailInfo",""],
    ["Response","{...}",""]
  ]);
  return table;
} 
我們表格內容先固定內容,所以先簡單一點直接設定 index 的值。
function setTableCellStyle(table){
 // 設定第一欄紅字的部份
 for(var i= 0; i<17; i++){
    if(i==1 || i>12){
      table.getRow(i).getCell(0).setBackgroundColor('#F3F3F3');
    }
    if(i==4 || i==5 ||  (i>7 && i<12)){
      table.getRow(i).getCell(0).editAsText()
        .setForegroundColor("#980000")
    }
  }
  // 欄位標題的背景色
  for(var i= 0; i<3; i++){
    table.getRow(3).getCell(i).setBackgroundColor('#DDDDDD');
    table.getRow(7).getCell(i).setBackgroundColor('#DDDDDD');
  }
  // 段落標題的背景色
  for(var i= 0; i<3; i++){
    table.getRow(0).getCell(i).setBackgroundColor('#BBB9B9').setBold(true);
    table.getRow(2).getCell(i).setBackgroundColor('#BBB9B9').setBold(true);
    table.getRow(6).getCell(i).setBackgroundColor('#BBB9B9').setBold(true);
    table.getRow(12).getCell(i).setBackgroundColor('#BBB9B9').setBold(true);
  }
}