線上書籍

Home

鐵人賽-Google Apps Script整合運用

建立首頁
  1. prog_index.gs => index(e) /*===================================== 首頁 =====================================*/ function index(e) { let title = '育將電腦'; let isAdmin = SCRIPT_PROP.getProperty('adminEmail') === Session.getActiveUser().getEmail() ? true : false; let menu = render('menu', {title: title, isAdmin: isAdmin}); return render('index', {menu: menu}, title); }

     

  2. doGet(e) /*======================================== doGet =========================================*/ function doGet(e){ return index(e); }

     

 

建立路由

路由也是全域物件,故請放在函式外面

  1. 路由物件
    route:流程指標關鍵字
    callback:函式
    建議:例 'custom' => custom(e) //------------------------------------- 2. 路由 var Route = {}; Route.path = function (route, callback) { Route[route] = callback; }

     

  2. 建立「客戶資料查詢」路由
    Route => ?op=custom
    函式 => custom(e)
    此時必須建立 custom(e),否則系統會報錯,說無此函式 // ----------------------------------- 客戶查詢 路由 Route.path("custom", custom);

     

  3. 調用路由
    用第2項講解
    如果有 Route['custom'] 則執行 Route['custom'](e) => custom(e)
    若沒有此 Route['custom'] 則執行首頁 index(e)
      //----------------------------------- 調用路由 if (Route[e.parameter.op]) { return Route[e.parameter.op](e); } else { return index(e); }
  4. prog_custom.gs => custom(e) /*======================================== 客戶資料 查詢 =========================================*/ function custom(e){ let title = '客戶資料 查詢'; let isAdmin = SCRIPT_PROP.getProperty('adminEmail') === Session.getActiveUser().getEmail() ? true : false; let menu = render('menu', {title: title, isAdmin: isAdmin}); return render('index', {menu: menu}, title); }

     

  5. 取得網頁應用程式網址:ScriptApp.getService().getUrl();
     

    ScriptApp.getService().getUrl()

     

  6. 請在選單,建立選單供管理員操作 <li><a class="dropdown-item" href="<?= url ?>?op=custom">客戶資料管理</a></li>

     

  7. 測試在沒「管理員」權限時,是否可以執行
  8. 目前有「首頁」、「客戶資料管理」有些變數需要宣告為「全域變數」,讓其可以在所有路由操作
搭配權限
  1. 假設「客戶資料」只給管理員操作,那請將建立路由,放在該條件裡面
  2. 請再測試權限
 doPost(e)