Home > database >  How do I make the variable bold in Google Apps Script?
How do I make the variable bold in Google Apps Script?

Time:03-11

I want to make bold a string stored in a variable called boldtext. I want a simple one line code for this. I am not from coding background. Please help

Edit 1: I made mistake in my original question. So, I am putting the question again.

My need = I want variable boldtext to be bold.

function calendar() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Course');
  var user = Session.getActiveUser().getUsername(); //gives user name
  var firstpart = user.split('.')[0]; //gives first part of user name
  var uppercase = firstpart.charAt(0).toUpperCase()   firstpart.substring(1);//makes first letter upper case
  var boldtext = 'Nice work, '   uppercase //This has to be made bold
  var bold  = SpreadsheetApp.newTextStyle().setBold(true).build();
  var boldname = SpreadsheetApp.newRichTextValue().setText(boldtext).setTextStyle(boldtext.indexOf(uppercase), boldtext.length, bold).build(); //mistake is here by me.
    spreadsheet.getRange('X53').setValue(boldname   'Reminder is added to your calendar');

CodePudding user response:

function myFunction() {
  var user  = Session.getActiveUser().getUsername();
  var first = user.split('.')[0];
  var name  = first[0].toUpperCase()   first.substring(1);
  var text  = 'Nice work, '   name;
  var bold  = SpreadsheetApp.newTextStyle().setBold(true).build();
  var value = SpreadsheetApp.newRichTextValue().setText(text).setTextStyle(text.indexOf(name), text.length, bold).build();

  SpreadsheetApp.getActiveSheet().getRange('A1').setRichTextValue(value);
}

Reference

Updated variant

function calendar() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Course');
  var user        = Session.getActiveUser().getUsername();
  var firstpart   = user.split('.')[0];
  var name        = firstpart[0].toUpperCase()   firstpart.substring(1);
  var boldtext    = 'Nice work, '   name;
  var bold        = SpreadsheetApp.newTextStyle().setBold(true).build();
  var text        = ' Reminder is added to your calendar';
  var value       = SpreadsheetApp.newRichTextValue().setText(boldtext   text).setTextStyle(0, boldtext.length, bold).build();
  spreadsheet.getRange('X53').setRichTextValue(value);
}
  • Related