javascript - How do I get the formatting the Current cell of the table in Word using office.js -
i had asked question how formatting of cell using office.js in excel. , have same question again time ms-word possible can formatted text there in table cell created in word application.
though able selected text html gives me styles needed
office.context.document.getselecteddataasync(office.coerciontype.html, function (result) { if (result.status === office.asyncresultstatus.succeeded) { shownotification('the selected text is:', '"' + result.value + '"'); } else { shownotification('error:', result.error.message); } });
i want current cell formatted text thank you!
excellent question pradeep. in order cell formatting need use currently-in-preview word 1.3 apis. can see how try 1.3 apis here. (note need use preview office.js cdn disclosed on page!) check of what's new here.
now, once ready try 1.3, following code give cell formatting information. in general code doing is
- verifies if selection within cell in table.
- once verification done, 'body' of cell. body objects contain font object include formatting properties need.(ie. color, font name, bold, italic etc. can html of body using gethtml() function.
find below code that. please note code return format applied in whole cell. if want go next level, go word word formatting info, need apply split method on body object, can traverse , formatting info of each of returned ranges. hope helpful! happy coding!!
function getformattedtext() { word.run(function (context) { //step 1: lets selection's range. , find out if within table cell..... var myselection = context.document.getselection().parenttablecell; context.load(myselection); return context.sync() .then(function () { if (myselection.isnull == true) { //selection not within cell..... console.log("selection not in header"); } else { // selection inside cell! lets content.... var body = myselection.body; context.load(body, { expand: 'font' }); return context.sync() .then(function () { console.log(body.font.name + " " + body.font.color); // @ point font properties available, bold, italic color.. etc... }) .catch(function (e) { console.log(e.message); }); } }) .catch(function (e) { console.log(e.message); }); }); }
Comments
Post a Comment