1. 在剛剛建立的 Google 試算表中,點選上方選單的「擴充功能」 -> 「Apps Script」。
2. 刪除 Apps Script 編輯器中所有預設的程式碼。
3. 複製並貼上下方提供的最新版程式碼到編輯器中。
// GAS 程式碼開始 (v5 - 新增說明欄位)
const SHEET_NAME = "上傳的網頁";
const HEADERS = ["ID", "檔案名稱", "HTML內容", "上傳時間", "說明"];
function getSheet() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
let sheet = ss.getSheetByName(SHEET_NAME);
if (!sheet) {
sheet = ss.insertSheet(SHEET_NAME);
sheet.appendRow(HEADERS);
sheet.setFrozenRows(1);
sheet.setColumnWidth(1, 150);
sheet.setColumnWidth(2, 200);
sheet.setColumnWidth(3, 100);
sheet.setColumnWidth(4, 200);
sheet.setColumnWidth(5, 300); // 新增說明欄的寬度
}
return sheet;
}
function handleResponse(data) {
return ContentService
.createTextOutput(JSON.stringify(data))
.setMimeType(ContentService.MimeType.JSON);
}
function doGet(e) {
try {
const action = e.parameter.action;
const sheet = getSheet();
if (action === "list") {
const data = sheet.getDataRange().getValues();
const files = data.slice(1).map(row => ({
id: row[0],
filename: row[1],
description: row[4] || '' // 回傳說明,如果為空則回傳空字串
}));
return handleResponse({ status: "success", data: files.reverse() });
}
if (action === "read") {
const id = e.parameter.id;
if (!id) return ContentService.createTextOutput("Error: Missing ID.");
const data = sheet.getDataRange().getValues();
const row = data.find(r => r[0] == id);
if (row) {
return ContentService.createTextOutput(row[2]).setMimeType(ContentService.MimeType.JAVASCRIPT);
} else {
return ContentService.createTextOutput("Error: Page not found.");
}
}
return ContentService.createTextOutput("Error: Invalid action.");
} catch (error) {
return handleResponse({ status: "error", message: error.message });
}
}
function doPost(e) {
try {
const sheet = getSheet();
const postData = JSON.parse(e.postData.contents);
const action = postData.action;
if (action === 'upload') {
const filename = postData.filename;
const content = postData.content;
let customId = postData.id;
const description = postData.description || ""; // 取得說明,若無則為空字串
// 驗證自訂ID格式
if (customId && !/^[a-zA-Z0-9-]+$/.test(customId)) {
return handleResponse({ status: "error", message: "自訂ID只能包含英文、數字和連字號 (-)" });
}
// 檢查ID是否重複
if (customId) {
const lastRow = sheet.getLastRow();
if (lastRow > 1) {
const ids = sheet.getRange(2, 1, lastRow - 1, 1).getValues().flat();
if (ids.includes(customId)) {
return handleResponse({ status: "error", message: `ID "${customId}" 已經存在,請換一個。` });
}
}
}
const id = customId || Utilities.getUuid();
const timestamp = new Date();
sheet.appendRow([id, filename, content, timestamp, description]); // 將說明寫入試算表
return handleResponse({ status: "success", id: id, filename: filename });
}
if (action === 'delete') {
const idToDelete = postData.id;
if (!idToDelete) return handleResponse({ status: "error", message: "缺少ID" });
const data = sheet.getDataRange().getValues();
const rowIndex = data.findIndex(row => row[0] == idToDelete);
if (rowIndex > 0) {
sheet.deleteRow(rowIndex + 1);
return handleResponse({ status: "success", message: "刪除成功" });
} else {
return handleResponse({ status: "error", message: "找不到要刪除的項目" });
}
}
return handleResponse({ status: "error", message: "無效的操作" });
} catch (error) {
return handleResponse({ status: "error", message: error.message });
}
}
// GAS 程式碼結束