|
/* |
|
* Highlight possibly demonetized words in yellow and likely demonetized words in orange |
|
* Source for demonetized words list: https://docs.google.com/spreadsheets/d/1ozg1Cnm6SdtM4M5rATkANAi07xAzYWaKL7HKxyvoHzk |
|
*/ |
|
function runDemonetizationChecker() { |
|
const { |
|
possiblyDemonetized, |
|
likelyDemonetized |
|
} = getSheetValuesFromId(); |
|
// Open the active document |
|
const doc = DocumentApp.getActiveDocument(); |
|
const body = doc.getBody(); |
|
|
|
// Loop through each word in the list |
|
for (let i = 0; i < possiblyDemonetized.length; i++) { |
|
highlightWord(body, possiblyDemonetized[i], "#fff2cc"); |
|
} |
|
|
|
for (let i = 0; i < likelyDemonetized.length; i++) { |
|
highlightWord(body, likelyDemonetized[i], "#ffd966"); |
|
} |
|
} |
|
|
|
function getSheetValuesFromId() { |
|
// URL of the Google Sheet |
|
const id = '1ozg1Cnm6SdtM4M5rATkANAi07xAzYWaKL7HKxyvoHzk'; |
|
|
|
// Open the spreadsheet by URL |
|
const spreadsheet = SpreadsheetApp.openById(id); |
|
|
|
// Get the first sheet by name |
|
const sheet = spreadsheet.getSheetByName('Demonetized Words'); |
|
|
|
const lastRow = sheet.getLastRow(); |
|
|
|
// Get the range of column A starting from A4 to the last row |
|
const range = sheet.getRange(4, 1, lastRow - 3, 1); // (row, column, numRows, numColumns) |
|
// Get the values from the range |
|
const values = range.getValues(); |
|
const backgrounds = range.getBackgrounds(); |
|
|
|
// Arrays to store values based on background color |
|
let possiblyDemonetized = []; |
|
let likelyDemonetized = []; |
|
|
|
// Iterate through the values and separate them by background color |
|
for (let i = 0; i < values.length; i++) { |
|
const cellValue = values[i][0]; |
|
const cellColor = backgrounds[i][0]; |
|
|
|
if (cellColor === "#fff2cc") { // Light yellow 3 in hex color |
|
possiblyDemonetized.push(cellValue.toString().toLowerCase()); |
|
} else if (cellColor === "#ffd966") { |
|
likelyDemonetized.push(cellValue.toString().toLowerCase()); |
|
} |
|
} |
|
|
|
return { |
|
possiblyDemonetized, |
|
likelyDemonetized |
|
}; |
|
} |
|
|
|
function highlightWord(body, word, color) { |
|
// Regex pattern to find words that start with a specified word and ends with at most two additional English characters |
|
const searchPattern = `\\b${word}[a-zA-Z]{0,2}\\b` |
|
// Create a text searcher for the word |
|
let searchResult = body.findText(searchPattern); |
|
|
|
// Highlight each occurrence of the word |
|
while (searchResult !== null) { |
|
const startOffset = searchResult.getStartOffset(); |
|
const endOffset = searchResult.getEndOffsetInclusive(); |
|
const textElement = searchResult.getElement(); |
|
|
|
// Apply background color to the word |
|
textElement.setBackgroundColor(startOffset, endOffset, color); // Yellow color |
|
// Search for the next occurrence |
|
searchResult = body.findText(searchPattern, searchResult); |
|
} |
|
} |