2019-06-24|閱讀時間 ‧ 約 4 分鐘

google excel script - 指令碼編輯器

心血來潮突然想用google excel來記錄每天的花費
有一些小地方想用指令碼來方便自己作業

開始

開啟google試算表
工具 - 指令碼編輯器
會開啟另一個分頁,就是在這邊編輯你的指令碼

程式碼

首先就是先得到你的 google spread sheet (google excel) 在哪邊
var ss = SpreadsheetApp.getActiveSpreadsheet(); // get 現在這個 google spread sheet
這邊 Example 做一個功能
我想要選取一個區域,然後按一下,讓這個區域的顏色變色(看起來很冗XD,練習用)
先創造出一個 menu 選單,這裡可以讓你把要做的功能給加進去
// The onOpenfunction create a menu function onOpen(){ var menuEntries = []; menuEntries.push({name: "SetColor", functionName: "function1"}); menuEntries.push(null); // line separator menuEntries.push({name: "Another", functionName: "function2"}); menuEntries.push(null); // line separator ss.addMenu("addMenuExample", menuEntries); }
  • 再來就是你所要做的功能,變色
var colorRef1 = ss.getRange('A1').getBackgroundColor(); //the color reference you put at A1 location function function1(){ var range = ss.getActiveRange(); // get range you selected range.setBackground(colorRef1); // the color reference range.setBorder(true, true, true, true, false, false);//setBorder(top, left, bottom, right, vertical, horizontal) console.log(range.getA1Notation()); }

補充
如果你是 1 個 excel 有許多分頁
並且要存取特定的分頁
var ss = SpreadsheetApp.getActiveSpreadsheet(); // get 現在這個 google spread sheet var sheet1 = ss.getSheetByName("測試"); // "測試" 為你的工作表分頁名稱,注意不是左上角的名稱
並且將上面程式碼的ss改成sheet1即可
另外這邊值得一提的是console.log,可以用來debug
console.log(你要 debug 的變數);
log的地方在
檢視-Stackdriver Logging
除了變色之外,想做什麼功能可以查 API
API document

執行

編輯好了之後,就按執行
他會需要你先存檔,就繼續存檔
他會需要你授權,就按 " 核對授權 "
會跳出一個 " 這個應用程式未經驗證 ", 不用怕, 就繼續前往, 然後允許
分享至
成為作者繼續創作的動力吧!
從 Google News 追蹤更多 vocus 的最新精選內容從 Google News 追蹤更多 vocus 的最新精選內容

發表回應

成為會員 後即可發表留言
© 2024 vocus All rights reserved.