I’m trying to create a Google Apps Script that will make text bold from the beginning of each line until it reaches a colon character. However, I keep getting an error about parameter mismatch.
function applyBoldFormatting() {
var document = DocumentApp.getActiveDocument();
var content = document.getBody();
var fullText = content.getText();
var textLines = fullText.split("\n");
for (var j = 0; j < textLines.length; j++) {
var currentLine = textLines[j];
var colonPosition = currentLine.indexOf(':');
console.log(colonPosition);
if (colonPosition !== -1) {
content.editAsText().setBold(j, 0, colonPosition, true);
}else{
console.log(colonPosition);
}
}
}
I get this error message:
Exception: The parameters (number,number,number,(class)) don’t match the method signature for DocumentApp.Text.setBold. applyBoldFormatting @ Script.gs:14
What’s wrong with my code and how do I fix this issue? I’m not sure why the setBold method isn’t accepting my parameters correctly.
The problem is you’re calculating text positions wrong. When you split text by lines, you lose track of where characters actually sit in the document. setBold needs absolute positions from the document start, not relative positions within each line.
I hit this exact issue formatting headings in reports. Instead of splitting into lines, use findText() to locate actual text ranges in the document.
Here’s what worked for me:
function applyBoldFormatting() {
var document = DocumentApp.getActiveDocument();
var body = document.getBody();
var searchPattern = '^[^:]+:';
var foundElement = body.findText(searchPattern);
while (foundElement != null) {
var foundText = foundElement.getElement().asText();
var start = foundElement.getStartOffset();
var end = foundElement.getEndOffsetInclusive();
foundText.setBold(start, end - 1, true); // exclude the colon
foundElement = body.findText(searchPattern, foundElement);
}
}
This searches for patterns from line start to colon and applies formatting using correct document positions.
yeah, setBold only takes start and end positions - ur passing 4 params. use setBold(startPos, endPos, true) instead of setBold(j, 0, colonPosition, true). also, splitting by lines messes up char positions. work with the raw text ranges directly.
Your setBold method has the wrong number of parameters. It should be setBold(startOffset, endOffset, isBold) but you’re passing four parameters including the line index. The real problem is you’re mixing line-based iteration with document-wide character positioning. When you split text into lines, character positions reset for each line, but setBold expects absolute positions from the document start. I’ve tackled similar formatting issues and had better luck using getParagraphs() instead of manually splitting text:
function applyBoldFormatting() {
var document = DocumentApp.getActiveDocument();
var body = document.getBody();
var paragraphs = body.getParagraphs();
for (var i = 0; i < paragraphs.length; i++) {
var paragraph = paragraphs[i];
var text = paragraph.getText();
var colonIndex = text.indexOf(':');
if (colonIndex > 0) {
paragraph.editAsText().setBold(0, colonIndex - 1, true);
}
}
}
This works within each paragraph’s scope and avoids the character offset confusion that’s causing your parameter error.