星期六, 5月 09, 2026

[GAS] claps 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 筆快取限制)

星期一, 4月 20, 2026

[SQL] 稀疏欄位 (Sparse Column)

稀疏欄位 (Sparse Column) 是 MS SQL 2008 功能,官方文件說明
Sparse columns are ordinary columns that have an optimized storage for null values. Sparse columns reduce the space requirements for null values at the cost of more overhead to retrieve non-NULL values. Consider using sparse columns when the space saved is at least 20 percent to 40 percent.
Sparse 關鍵字
USE AdventureWorks2025;  
GO  
  
CREATE TABLE DocumentStore  
    (DocID int PRIMARY KEY,  
     Title varchar(200) NOT NULL,  
     ProductionSpecification varchar(20) SPARSE NULL,  -- SPARSE Column 
     ProductionLocation smallint SPARSE NULL,          -- SPARSE Column
     MarketingSurveyGroup varchar(20) SPARSE NULL ) ;  -- SPARSE Column
GO  
Sparse Column 查詢
use AdventureWorks2025
go 

SELECT 
	[name] AS ColumnName ,
	is_nullable ,
	is_sparse ,
	is_column_set
FROM sys.columns
WHERE object_name(object_id) =  'DocumentStore'
限制
  • Sparse columns are a property of the storage layer, rather than the logical table. Therefore a SELECT ... INTO statement does not copy over the sparse column property into a new table.
  • A sparse column must be nullable and cannot have the ROWGUIDCOL or IDENTITY properties. A sparse column cannot be of the following data types: text, ntext, image, timestamp, user-defined data type, geometry, or geography; or have the FILESTREAM attribute.
  • A sparse column cannot have a default value.
  • Sparse columns are incompatible with data compression. Therefore sparse columns cannot be added to compressed tables, nor can any tables containing sparse columns be compressed.
  • Changing a column from sparse to non-sparse, or non-sparse to sparse, requires changing the storage format of the column. The SQL Server Database Engine uses the following procedure to accomplish this change: 
    •  Adds a new column to the table in the new storage size and format. 
    •  For each row in the table, updates and copies the value stored in the old column to the new column. 
    •  Removes the old column from the table schema. 
    •  Rebuilds the table (if there is no clustered index) or rebuilds the clustered index to reclaim space used by the old column.
以上只列出較感興趣限制,完整限制說明可以查看 Restrictions for using sparse columns

重構某單位 AP 時發現 Table 整併後,配合商業邏輯而新增欄位內有 90% 資料會是 null,原本是想要多拆一個 Table 來存放節省空間,但該情境似乎蠻符合 Sparse Column 就來使用看看囉