Answer the question
In order to leave comments, you need to log in
How to count how many occurrences of each word in a table (or cell)?
https://docs.google.com/spreadsheets/d/1RrMOuBr-Eq...
There is a set of reviews, I want to determine how many words are found.
Those. not certain words, but count all the words - how many each of them occurs.
Is there such a universal formula?
Answer the question
In order to leave comments, you need to log in
Victor L was right.
If you change its code a little, you can get not by one cell, but by range.
function COUNTUNIQWORDS(range) {
let wordsCount = {};
range.forEach(row=>{
row.forEach(cell=>{
let text = String(cell)
.toLowerCase()
.replace(/[^а-яА-ЯёЁa-zA-Z ]+/gi, ' ')
.replace(/\s+/gi, ' ');
let words = text
.split(' ')
.filter(word=>word!=='');
words.forEach(word=>{
if (wordsCount[word]){
wordsCount[word]+=1;
}else{
wordsCount[word]=1;
};
});
});
});
let result = Object.keys(wordsCount)
.sort()
.map(word=>[word, wordsCount[word]]);
return result;
};
You can create your own formula, for example
function COUNTUNIQWORDS(cell) {
var result = []
var words = String(cell).split(" ");
var wordsCount = words.reduce(function (acc, w) {
if(acc[w]) {
acc[w] += 1
} else {
acc[w] = 1;
}
return acc;
}, {});
for (var w in wordsCount) result.push('Cлово "'+ w +'" встречается '+ wordsCount[w] + ' раз');
return result
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question