JSON.stringify()
JSON.parse()
// 指令碼屬性:設定
SCRIPT_PROP.setProperty(屬性,值);
// 指令碼屬性:取值
SCRIPT_PROP.getProperty(屬性);
// Session: 取得當前使用者email(只限部署人)
Session.getActiveUser().getEmail();
// 網頁應用程式網址
ScriptApp.getService().getUrl()
//------------------------------------- 3. 全域變數
var global = get_global();
//------------------------------------- 4. 選單
var menu = render('menu', {});
取得全域變數
/*=====================================
取得全域變數
=====================================*/
function get_global() {
// 從指令碼屬性 取得 global
global = SCRIPT_PROP.getProperty('global') ? JSON.parse(SCRIPT_PROP.getProperty('global')) : {};
// 管理員權限
global['isAdmin'] = SCRIPT_PROP.getProperty('adminEmail') === Session.getActiveUser().getEmail() ? true : false;
// 網頁應用程式網址
global['url'] = ScriptApp.getService().getUrl();
return global;
}
設定全域變數
/*=====================================
安裝程式
=====================================*/
function setup() {
//管理員email
SCRIPT_PROP.setProperty("adminEmail", Session.getActiveUser().getEmail());
global = {
webTitle : '育將電腦工作室'
}
SCRIPT_PROP.setProperty("global", JSON.stringify(global));
global = get_global();
}
確認 global 沒問題後,請更改樣版語法 menu.html
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="<?= global.url ?>"><?= global.webTitle ?></a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="<?= global.url ?>">首頁</a>
</li>
<? if(global.isAdmin){ ?>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
管理員
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<li><a class="dropdown-item" href="<?= global.url ?>?op=custom">客戶管理</a></li>
<li><hr class="dropdown-divider"></li>
</ul>
</li>
<? } ?>
</ul>
</div>
</div>
</nav>
調整樣版 將index.html 改名 tmp.html
<html>
<head>
<meta charset="utf-8">
<base target="_top">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- bootstrap -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-HwwvtgBNo3bZJJLYd8oVXjrBZt8cqVSpeBNS5n7C8IVInixGAoxmnlMuBnhbgrkm" crossorigin="anonymous"></script>
<!-- bootstrap icon -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css">
<!-- jquery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script>
</head>
<body>
<?!= menu ?>
<div class="container">
<!-- 資料處理時畫面 -->
<div class="d-none my-5" id="show">
<span>資料處理中....<img src="https://i.imgur.com/pm9SKUg.gif"></span>
</div>
<!-- 主畫面 -->
<div id='main-content'>
<?!= content ?>
</div>
</div>
</body>
</html>
index(e)
/*=====================================
首頁
=====================================*/
function index(e) {
let title = '首頁';
let content = '';
content = `<h2 class='mt-3'>${title}</h2>`;
return render('tmp', {content: content}, title);
}
custom(e)
/*========================================
客戶資料 查詢
=========================================*/
function custom(e){
let title = '客戶管理';
let content = '';
content = `<h2 class='mt-3'>${title}</h2>`;
return render('tmp', {content: content}, title);
}
// ----------------------------------- 管理員 路由
if(global.isAdmin){
Route.path("custom", custom);
}
// ----------------------------------- 管理員 路由 END