add in - Get Current rows cells values one by one on "BindingSelectionChanged" event from table in html form in Word using office.js -
hello creating application using office.js used in excel , word application addin , have written code gives text of entire row cell cell. requirement maintain styles , of every cell , store them in database when again addin runs should load data in same format stored. text getting in response. have asked similar question text styles current cell works great. how formatting current cell of table in word using office.js
there thing if possible cell html row , column position solve problem. thank you!
hi found solution of problem solution word not working in excel working me writing here :-
function addtable() { var document = office.context.document; var headers = [["cities"]]; var rows = [['<b>hello there</b> <ul><li>one</li><li>two</li></ul>'], ['roma'], ['tokyo'], ['seattle']]; var html = '<table>'; html += '<thead>'; (var = 0; < headers.length; i++) { html += '<tr>'; var cells = headers[i]; (var j = 0; j < cells.length; j++) { html += '<th>' + cells[j] + '</th>'; } html += '</tr>'; } html += '</tr>'; html += '</thead>'; html += '<tbody>'; (var = 0; < rows.length; i++) { html += '<tr>'; var cells = rows[i]; (var j = 0; j < cells.length; j++) { html += '<td>' + cells[j] + '</td>'; } html += '</tr>'; } html += '</tbody>'; html += '</table>'; office.context.document.setselecteddataasync(html, { coerciontype: office.coerciontype.html }, function (asyncresult) { document.bindings.addfromselectionasync(office.bindingtype.table, function (result) { console.log(result); var binding = result.value; binding.addhandlerasync(office.eventtype.bindingselectionchanged, onbindingselectionchanged); }); }); }
the above function call when want generate table html values in it.
and bellow is code using value of current cell , replacing dummy value.
var onbindingselectionchanged = function (results) { if (!isexecuted) { word.run(function (context) { var tablecell = context.document.getselection().parenttablecell; context.load(tablecell); return context.sync() .then(function () { if (tablecell.isnull == true) { //selection not within cell..... console.log("selection not in header"); } else { // selection inside cell! lets content.... var body = tablecell.body; var html = tablecell.body.gethtml(); var tablehtml = tablecell.body.gethtml(); context.sync() .then(function () { var cellhtml = html.value; var newhtml = "<table><tr><td><ul><li>yellow</li></ul></td></tr></table"; // option 1 body.inserthtml(newhtml, word.insertlocation.replace); // option 2 //body.clear(); //body.inserthtml(newhtml, word.insertlocation.end); return context.sync().then(function () { console.log('html replaced.'); }).catch(function (e) { console.log(e.message); }); }).catch(function (e) { console.log(e.message); }); } }).catch(function (e) { console.log(e.message); }); }); isexecuted = true; } else { isexecuted=false; } };
thank you!
Comments
Post a Comment