How to change font to bold without changing font size, font family

Posted by: vinayakbagi on 3 June 2020, 11:58 pm EST

  • Posted 3 June 2020, 11:58 pm EST

    Hi Team,

    In below implementation I have to compulsory give font size, font family.

    sheet.getCell(6, 1).text(‘Bold’).font(‘bold normal 15px Georgia’);

    Is there any way in which I can get current style data for selected cell and then just add or change bold/italics/underline properties without affecting current font size, font style etc.

  • Posted 5 June 2020, 1:00 am EST

    Hi,

    You may use a temporary span element to modify only part of the font string. Please refer to the following code snippet and attached sample which demonstrates the same:

    let oldFont = sheet
        .getCell(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex())
        .font(); //get the oldFon for the ActiveCell
      let newFont = getModifiedFontString(oldFont, "fontWeight", "bold");
      sheet
        .getCell(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex())
        .font(newFont); //update the newFont
    
    function getModifiedFontString(oldFontString, property, value) {
      var span = document.createElement("span");
      span.style.font = oldFontString;
      span.style[property] = value;
      return span.style.font;
    }
    
    

    sample: https://codesandbox.io/s/silly-kare-y3yno?file=/src/index.js

    Regards

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels