x
No Plagiarism!KMPWNQDlVFfuvq2Fh6N7posted on PENANA
是這樣的,我好奇我平均每小時的打字速度,於是我打算找個方法去偵測它。由於我常使用的文書工具是 Google docs,所以我首先想到的是利用docs的擴充功能。經過長時間搜尋,我總算找到幾個看似可用的外掛——"Word Counter Max for Google Docs"和"Writing Habit",都是外國人創造的。8964 copyright protection117PENANA0UZQC4VIlm 維尼
然而,這導致了一個問題:它們倆都不能紀錄中文字數,只能是英文字數——我的天,你們敢信,英文字數在這兩個程式中對應了中文的段落數。如果我想要記錄中文字數,應該去找「字元數」——這在英文中是word vs character的概念。8964 copyright protection117PENANA5upU7CqyW1 維尼
該死啊,我明明在剛用docs的時候就遇到過同樣的問題,現在又碰到一次,我還是幾分鐘之後才意識到。同一個坑踩了兩次!兩次!!8964 copyright protection117PENANAm9wCOWF7oZ 維尼
頭痛,於是我就想能不能用程式去計算呢?我想到了另一個擴充功能: Apps Scrpt。但是,它是java語言,不是我學過的c++和python。無奈,我只得求助ChatGpt。8964 copyright protection117PENANAyKmQXlUvtV 維尼
又是同樣的問題,ChatGpt來自國外,很常搞混字數和字元數的意思。明明上面寫「如果你想記錄 字元數(Character Count) 而不是字數(Word Count),只需要稍微修改程式碼:……」,然後程式又給我計算字數,我真的氣死。8964 copyright protection117PENANAh0m8e9Orwu 維尼
於是我只能上網爬文查詢函式了。費了千辛萬苦,我總算成功顯示字元數啦~。8964 copyright protection117PENANAzk75u3LWvq 維尼
但是三小,為什麼數字不一樣??!!
8964 copyright protection117PENANAXOIzfA6Bul 維尼
圖一:來自google docs8964 copyright protection117PENANAaRhmVfEBrk 維尼
8964 copyright protection117PENANAfieSbQCmgW 維尼
圖二:來自Apps Scrpt8964 copyright protection117PENANAsdqp4N3m2w 維尼
兩個字,大中風!121Please respect copyright.PENANAxm8szkPTbt
121Please respect copyright.PENANAfFG45auQdB
總之我不玩了,大學做程式模擬的煩躁感爬上我的背脊,很躁!8964 copyright protection117PENANAcWI2ElpQr5 維尼
以下附上我現在的code,看誰想嘗試嘗試?8964 copyright protection117PENANAlEAxTHomo9 維尼
function trackWordCount() {8964 copyright protection117PENANAJ5Ui67kZq0 維尼
try {8964 copyright protection117PENANAbG0U62nOjP 維尼
var docId = "1wWNbQtmKMx97g1PvkQPYWWCk7aUGhAkDQ-63XUa25g0"; // 替換成你的 Docs ID8964 copyright protection117PENANAv2l3dtHszr 維尼
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("test1");8964 copyright protection117PENANASf4Yw42xQ4 維尼
// 讀取 Google Docs 內容8964 copyright protection117PENANAAtTAb5y9ba 維尼
var doc = DocumentApp.openById(docId);8964 copyright protection117PENANApxJso5tHjY 維尼
var text = doc.getBody().getText();8964 copyright protection117PENANA3qf0qcLtSY 維尼
//var wordCount = text.split(/\s+/).filter(word => word.length > 0).length;8964 copyright protection117PENANADSoEITPIdA 維尼
var charCount = text.replace(/\s/g, "").length; // 計算不含空格的字元數8964 copyright protection117PENANAGdV5kO80Lf 維尼
// 讀取上一個記錄的字元數8964 copyright protection117PENANAOlZ1DMi8iY 維尼
var lastRow = sheet.getLastRow();8964 copyright protection117PENANAkRTozbJ58k 維尼
var lastCharCount = lastRow > 1 ? sheet.getRange(lastRow, 2).getValue() : 0;8964 copyright protection117PENANA56EL9BSS6V 維尼
var charsAdded = charCount - lastCharCount;8964 copyright protection117PENANAU60TymfJcR 維尼
// 記錄到試算表8964 copyright protection117PENANAgLWkEqnBx9 維尼
sheet.appendRow([new Date(), charCount, charsAdded]);8964 copyright protection117PENANAosjqGAbwta 維尼
Logger.log("成功記錄字數:" + charCount);8964 copyright protection117PENANAQjGCx2ocAo 維尼
} catch (error) {8964 copyright protection117PENANAuNwkulNnss 維尼
Logger.log("錯誤:" + error.message);8964 copyright protection117PENANAGrno1KHWZi 維尼
}8964 copyright protection117PENANAzc8iWAI1jB 維尼
}8964 copyright protection117PENANA53fk9eOZws 維尼
18.226.159.125
ns18.226.159.125da2