iT邦幫忙

2021 iThome 鐵人賽

DAY 23
0
IT管理

「Google Apps Script」 學習筆記系列 第 23

【Day 23】Google Apps Script - API Blueprint 篇 - Google Docs 轉換 API Blueprint 格式(1)

來練習把寫在 Google Docs 裡 查詢Gmail資訊 API 表格的轉成 API Blueprint 格式吧。


今日要點:
》Google Docs 轉換 API Blueprint 格式之一


Google Docs 轉換 API Blueprint 格式之一

在前置作業與介紹都完成後,今天終於要來撰寫 Google Apps Script 程式碼來進行格式轉換了。我們在

  • 【Day 7】使用 Google Docs 撰寫 Demo 用的 API 文件
  • 【Day 8】依此文件實作 Demo 用的 API
  • 【Day 9】部署網頁應用程式得到 API 的 URL
  • 【Day 11】介紹了轉換流程架構與相關服務
  • 【Day 16】Document Service - 文件服務範例-讀取表格
  • 【Day 22】API Blueprint 格式範例

經過上述幾天的介紹,我們已經把轉換時所要的資訊都準備好了。
可以開始把 【Day 7】介紹的 Google Docs 轉換成 【Day 22】 API Blueprint 格式。

程式碼

我們程式碼大概分幾個部份

  • 全域變數定義
  • 主要入口,讀取 google Document 檔案
  • 取得所有的表格集合(tables),處理每個表格
  • 處理表格每一列的內容
  • 處理 API 的描述區塊
  • 處理 Action 區塊

程式碼如下:

全域變數定義

var gApibConent ="";  //要存到apib檔的內容
const  NL = "\n";  //NEWLINE
const  REPLACE_URL = "REPLACE_URL";
const  HOST = "https://script.google.com";
const  VER = "v1.0"; 

//各段標題的起始列
var gSegmentTitle = ["Request Url Params", "Response Body", "Sample"];
var gSegmentIndex = [-1, -1, -1];
var gSegmentColspan = [3, 3, 2];

 

主要入口

///////////////////////////////////////////////////////
//主要入口
///////////////////////////////////////////////////////
function doGet(e){
  gApibConent =
              "HOST: " + HOST   + NL + 
              "FORMAT: 1A"      + NL + NL +
              "# 鐵人賽 2021 "   + NL + 
              "## Version"      + NL + 
              VER               + NL +  
              NL ;  

  readDocById('1AvS30VgjUNSYwg_sEpOd......');  //鐵人賽 API 文件 Demo
  createGoogleDriveTextFile(gApibConent);
  Logger.log('轉換完成');
}

 


讀取 google Docs 檔案, 處理每個表格

///////////////////////////////////////////////////////
//讀取 google Dos 檔案, 處理每個表格
///////////////////////////////////////////////////////
function readDocById(id) {

  var doc = DocumentApp.openById(id);  
  var tables = doc.getBody().getTables();
  var docfilename = doc.getName();
  Logger.log("開始轉換 docfilename = " + docfilename);

  var groupTitle = docfilename.split("-")[1]; 
  gApibConent = gApibConent + NL + "# Group "+ groupTitle + NL;    

  Logger.log("groupTitle = " + groupTitle);


  //讀取每一個表格
  for (var tableIndex in tables)
  {
    var table = tables[tableIndex];     //取出第 n 個 table
    var tablerows = table.getNumRows(); //取得有幾列
    gSegmentIndex = [-1, -1, -1, -1];

    //讀取表格中的每一列
    var array = [];
    for ( var row = 0; row < tablerows; ++row ) {
      var tablerow = table.getRow(row)

      //讀取一列中的每一格Cell
      array[row] = [];
      for ( var cell=0; cell < tablerow.getNumCells(); ++cell) {
        var celltext = tablerow.getChild(cell).getText();

        array[row][cell] = celltext;
      }
        parseRowData(row, array);
    }

    //處理 API 的描述區塊
    makeApiDescription(array, tablerows);    
      
    //處理 URL 置換
    var sampleURL = array[(gSegmentIndex[2]+2)][1].replace(/ /g, "").replace(HOST, "");
    sampleURL = sampleURL + "{?name,func}";
    gApibConent = gApibConent.replace(REPLACE_URL, sampleURL) ;  

    //處理 Actions 區塊
    gApibConent = gApibConent + makeActions(array);
  }
}

 


處理 API 的描述區塊

function makeApiDescription(array, tablerows){

  //處理基本資料的 table 內容
  gApibConent = gApibConent + makeHeaderTable(array.slice(0, 2));  

  //處理各區塊的 table 內容
  if(gSegmentIndex[0]>-1){ 
    if(gSegmentIndex[1]>-1){
      gApibConent = gApibConent + makeTable(gSegmentColspan[0], array.slice(gSegmentIndex[0], gSegmentIndex[1]));
    } else if(gSegmentIndex[2]>-1){
      gApibConent = gApibConent + makeTable(gSegmentColspan[0], array.slice(gSegmentIndex[0], gSegmentIndex[2]));
    } else if(gSegmentIndex[3]>-1){
      gApibConent = gApibConent + makeTable(gSegmentColspan[0], array.slice(gSegmentIndex[0], gSegmentIndex[3]));
    }
  }

  if(gSegmentIndex[1]>-1){ 
    if(gSegmentIndex[2]>-1){
      gApibConent = gApibConent + makeTable(gSegmentColspan[1], array.slice(gSegmentIndex[1], gSegmentIndex[2]));
    }
  }

  if(gSegmentIndex[2]>-1){ 
    gApibConent = gApibConent + makeTable(gSegmentColspan[2], array.slice(gSegmentIndex[2], tablerows));
  } 
}

以上就是 Google Docs 轉換 API Blueprint 格式主要程式流程介紹,明天繼續把副程式介紹完,今天就先這樣囉。/images/emoticon/emoticon06.gif

參考


上一篇
【Day 22】Google Apps Script - API Blueprint 篇 - API Blueprint 格式範例
下一篇
【Day 24】Google Apps Script - API Blueprint 篇 - Google Docs 轉換 API Blueprint 格式(2)
系列文
「Google Apps Script」 學習筆記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言