星期二, 5月 12, 2026

[SQL] 轉型為 bit

無意中在自家系統內看見下述 TSQL 語法,整數 2 轉型 bit 竟然不會出現轉型錯誤
SELECT CAST(2 as bit)
在官方文件 - bit 內有這段說明
The bit data type can be used to store Boolean values. The string values TRUE and FALSE can be converted to bit values: TRUE is converted to 1, and FALSE is converted to 0.

Converting to bit promotes any nonzero value to 1.

The bit data type supports the COUNT function. However, other standard aggregate functions, like SUM, AVG, MIN, and MAX, don't directly support the bit data type.
基本上只要不是 0 就是轉型為 1 就是,測試範例如下
SELECT 
    CAST(source as bit) AS 轉型bit
FROM
    (
        SELECT 0 AS source UNION ALL
        SELECT 1 UNION ALL
        SELECT 2 UNION ALL 
        SELECT 100 UNION ALL
        SELECT -1 
    ) AS T

星期六, 5月 09, 2026

[GAS] clasp deploy

A release that makes a specific version of your script available for users. A deployment has a unique URL or ID.
GAS IDE 部屬有三種模式,分別為
  • 新增部屬作業:詳見 [GAS] 部屬網頁程式
  • 管理部屬作業:詳見 [GAS] 管理部屬作業
  • 測試部屬作業:未部屬狀態下可以執行 Code,會得到一個 dev 結尾網址,該位址不會變化,Code 有進行任何更改的話,可以在瀏覽器上按 F5 refresh 執行程式

而 claps deploy 有兩種語法應用
  • clasp deploy 對應 [新增部屬作業],部屬後每次都會取得一個新的網址
  • clasp deploy --deploymentId <deployment-id> 對應 [管理部屬作業],部屬後網址不變

星期二, 5月 05, 2026

[GAS] 專案紀錄

學習 clasp 時發現有 clasp version 語法可以使用,但是腦海裡對應不上是 GAS IDE 上哪一個功能

官方文件 - 部署作業與版本 說明
A static snapshot of your script project's code. Once created, a version is immutable. Think of a version as a "save point" in your development history.
仔細去查發現,原來在 GAS IDE 上繁體中文是 [專案紀錄],想說會不會是翻譯過來時差太多,特別切成英語版本來查看,還真的是 Project History

星期一, 5月 04, 2026

[GAS] Template 解析異常

請 AI 寫小範例驗證功能時,發生下述錯誤訊息
SyntaxError: Unexpected token ';' (第 18 行,檔案名稱:程式碼)
該範例就單純在 code.gs 內的參數傳遞至 Index.html 而已,因為錯誤訊息一直指向 code.gs 內,所以第一時間也沒有意識到錯誤是在 Index.html 內,都修錯方向

部屬執行後都出現下面錯誤訊息
錯誤竟然在 Index.html 內的註解,傻眼


正確應該是下圖才是,這樣才會正確解析 < 和 > 符號,但這是註解說,本來就沒有要顯示,真的打在有趣的點

星期二, 4月 28, 2026

[GAS] CacheService

Google Apps Script 本身有快取服務 CacheService 可以直接使用,分別為
  • getDocumentCache()
  • getScriptCache()
  • getUserCache()
該筆記以 getScriptCache() 為目標筆記,文件說明
Gets the cache instance scoped to the script. Script caches are common to all users of the script. Use these to store information that is not specific to the current user.
CacheServide 最多儲存 1,000 筆,超過 1,000 筆的話會保留 expirationInSeconds 最長的 900 筆資料

CRUD Sample Code

直接請 AI 產出紀錄
/**
 * 示範 Google Apps Script 的 CacheService 混合寫入操作
 * 包含 putAll (批量新增)、put (單筆新增)、getAll (批量讀取)、刪除與驗證
 * * @returns {void}
 */
function demoCacheServiceMixedCRUD() {
  // 1. 初始化 ScriptCache
  const cache = CacheService.getScriptCache();
  
  // 2. 使用 putAll() 批量新增三筆資料
  // 將快取資料整理成一個 Object (KeyValue)
  const batchData = {
    'user_01': 'Alice',
    'user_02': 'Bob',
    'user_03': 'Charlie'
  };
  // 寫入批次資料,設定存活時間為 600 秒 (預設值)
  cache.putAll(batchData, 600);
  console.log('✅ 已使用 putAll() 成功新增三筆資料至 Cache。');

  // 3. 使用 put() 追加新增單一筆資料
  const singleKey = 'user_04';
  cache.put(singleKey, 'David', 600);
  console.log(`✅ 已使用 put() 成功追加一筆資料至 Cache:${singleKey}`);

  // 4. 取得全部資料 (Read)
  // 將批次寫入的 keys 與單筆寫入的 key 合併成一個陣列來查詢
  const allKeysToFetch = ['user_01', 'user_02', 'user_03', 'user_04'];
  const allData = cache.getAll(allKeysToFetch);
  console.log('🔍 取得目前的資料狀態:', allData);

  // 5. 移除一筆資料 (Delete)
  const targetToRemove = 'user_02'; // 準備移除 Bob
  cache.remove(targetToRemove);
  console.log(`🗑️ 已執行移除動作,目標:${targetToRemove}`);

  // 6. 取得上一筆移除資料,驗證該筆資料已不存在
  const removedData = cache.get(targetToRemove);
  
  if (removedData === null) {
    console.log(`⚠️ 驗證成功:${targetToRemove} 已經被移除 (查詢結果為 null)。`);
  } else {
    console.log(`❌ 錯誤:${targetToRemove} 依然存在,值為:`, removedData);
  }
}
put(key, value, expirationInSeconds)
  • key:最多 250 字元
  • value:每一筆資料最大是 100KB
  • expirationInSeconds:預設值為 600 秒 (10 分鐘) ,最少為 1 秒、最長為 21,600 秒 (6 小時),指定的到期時間僅供參考,如果快取大量資料,系統可能會提前移除快取資料 (有 1,000 筆快取限制)