Skip to content

Instantly share code, notes, and snippets.

@mingfengwan
Last active October 11, 2024 15:15
Show Gist options
  • Save mingfengwan/44d2bc2b776be6f5c99939da74992916 to your computer and use it in GitHub Desktop.
Save mingfengwan/44d2bc2b776be6f5c99939da74992916 to your computer and use it in GitHub Desktop.
Google Apps Script to Check for YouTube Demonetization Words in Google Docs

YouTube Demonetization Checker

A Google Apps Script to highlight potentially demonetized words in Google Docs.

Features

  • Highlights possibly demonetized words in yellow.
  • Highlights likely demonetized words in orange.

Installation

  1. Open Google Docs.
  2. Click on Extensions > Apps Script.
  3. Copy and paste the script.

Usage

  1. Save and run the function from the Apps Script editor to check your document for demonetized words.
  2. Allow the necessary permissions for your project to edit the Google Doc and access the spreadsheet.
/*
* 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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment