我只是想計算 Google 檔案中的字符(而不是單詞)。它不能在此用例的活動檔案中,因此需要 post getBody。
到目前為止,這是我非常零散的腳本。對于背景關系,我希望在檔案末尾將一些功能集成到協作者的檔案中。從我目前所看到的情況來看,我需要字符數( 1)來實作這一點。
function myFunction() {
var doc = DocumentApp.openById([ID]);
var bodyCount = doc.getBody().getText();
var body = doc.getBody();
//var numChildren = body.getNumChildren();
//var pos = doc.newPosition(body.getChild(numChildren - 1),0);
var text = body.editAsText();
var space = " ";
//var text = DocumentApp.getActiveDocument().getBody().getText();
var words = bodyCount.replace(/\s /g, space).split(space);
Logger.log(words.length);
const characters = words.join('');
Logger.log(characters);
var charCount = characters.toString();
Logger.log(charCount);
var realCharCount = charCount.length();
//body.setCursor(pos);
text.insertText(words.length,"yadayadayada");
}
只是想插入文本,它需要是位置AKA的整數,字符數的 1。到目前為止,我擁有我需要的大部分功能,除了獲得一個數字:字符數。
這不是對單元格中的字符進行計數,也不是與作業表有任何關系。那會很好,因為我可以只使用長度函式......但到目前為止我不能在這種情況下。
uj5u.com熱心網友回復:
字符數:
function getCharacterCount() {
const ch = "ABCDEFGJHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";//You can add or remove what you wish to call legal characters this way
const doc = DocumentApp.openById();
const body = doc.getBody();
let txt = body.getText();
let s = txt.split('').filter(c => ~ch.indexOf(c)).filter(e => e);
let l = s.length;
Logger.log(l);
return l;
}
uj5u.com熱心網友回復:
使用 DocumentApp 和 SpreadsheetApp 時幾乎沒有什么不同,您需要做的就是將文本作為字串獲取,而不是使用 .length 獲取字符長度。
Google Apps Script 基本上將它們的函式命名為非常簡單,您可以閱讀檔案,或者在許多情況下只是console.log一個物件來檢查可以應用哪些方法。
在這種情況下,一個簡單的.getText()方法似乎就足夠了。
function countChar() {
const body = DocumentApp.getActiveDocument().getBody();
const text = body.editAsText();
const charCount = text.getText().length;
return charCount;
}
但請記住,每個空格或換行符也算作字符。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/529453.html
標籤:谷歌应用脚本谷歌文档
上一篇:二維陣列中的元素總和
