來練習把寫在 Google Docs 裡
查詢Gmail資訊API 表格的轉成 API Blueprint 格式吧。
今日要點:
》Google Docs 轉換 API Blueprint 格式之一

在前置作業與介紹都完成後,今天終於要來撰寫 Google Apps Script 程式碼來進行格式轉換了。我們在
經過上述幾天的介紹,我們已經把轉換時所要的資訊都準備好了。
可以開始把 【Day 7】介紹的 Google Docs 轉換成 【Day 22】 API Blueprint 格式。
我們程式碼大概分幾個部份
程式碼如下:
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 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);
  }
}
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 格式主要程式流程介紹,明天繼續把副程式介紹完,今天就先這樣囉。