Exception: The parameters (number,number,number,(class)) don’t match the method signature for DocumentApp.Text.setBold. makeBoldUntilColon @ Script.gs:14
What’s causing this and how can I fix it? Thanks for any help!
hey there! i’ve run into this before. the problem is ur trying to use the body’s editAsText() method, which doesnt work like that. instead, u gotta go through each paragraph separately. heres a quick fix:
function makeBoldUntilColon() {
var doc = DocumentApp.getActiveDocument();
var paras = doc.getBody().getParagraphs();
paras.forEach(function(p) {
var txt = p.getText();
var colon = txt.indexOf(':');
if (colon > -1) p.editAsText().setBold(0, colon, true);
});
}
give that a shot and lemme know if it works for ya!
I’ve dealt with similar formatting challenges in Apps Script before. The issue you’re facing stems from how you’re addressing the text in the document. Instead of working with the entire body text at once, it’s more effective to process each paragraph separately.
Here’s an alternative approach that should resolve your problem:
function makeBoldUntilColon() {
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
for (let i = 0; i < body.getNumChildren(); i++) {
const para = body.getChild(i).asParagraph();
const text = para.getText();
const colonIndex = text.indexOf(':');
if (colonIndex > -1) {
para.editAsText().setBold(0, colonIndex, true);
}
}
}
This method iterates through each paragraph in the document, finds the colon’s position, and applies bold formatting up to that point. It should resolve the parameter mismatch error you encountered. Give it a try and see if it works for your needs.
I encountered a similar issue when working with Apps Script and Google Docs. The problem lies in how you’re accessing the text to make it bold. Instead of using the body’s editAsText() method, you should work with each paragraph individually.
Here’s a modified version that should work:
function makeBoldUntilColon() {
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
const paragraphs = body.getParagraphs();
paragraphs.forEach(paragraph => {
const text = paragraph.getText();
const colonIndex = text.indexOf(':');
if (colonIndex > -1) {
paragraph.editAsText().setBold(0, colonIndex, true);
}
});
}
This approach iterates through each paragraph, finds the colon index, and applies bold formatting if a colon is present. It avoids the index mismatch error you were encountering. Give it a try and let me know if it solves your problem!