Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserving Original Text Style When Modifying Cell Content #5221

Closed
Raj-Dev1 opened this issue Dec 23, 2024 · 12 comments
Closed

Preserving Original Text Style When Modifying Cell Content #5221

Raj-Dev1 opened this issue Dec 23, 2024 · 12 comments
Assignees
Labels
Area: Word Issue related to Word add-ins Needs: author feedback Waiting for author (creator) of Issue to provide more info Status: no recent activity Issue or PR is stale (no recent activity)

Comments

@Raj-Dev1
Copy link

Platform : PC desktop
Host: Word
Office version number: Microsoft® Word for Microsoft 365 MSO (Version 2411 Build 16.0.18227.20082) 64-bit
Operating System: Microsoft Windows 11 pro

Hi team,
I applied a style to the text, but when I cleared the cell's content and inserted new text and content control into the cell, the styling (font size and font family) changed. I want the text to retain its original style rather than inheriting the parent-level style. Is there a way to preserve the original style?

const docCC = context.document.contentControls;
docCC.load("items");
await context.sync();

var findTbl = docCC.items.filter(a => a.tag == selectedTblId);
var table = findTbl[0].parentTable;
context.load(table, 'rows/items/rowIndex,rowCount,rows/items/cells/items/body/font/superscript,rows/items/cells/items/body/contentControls/items/tag,rows/items/cells/items/body/contentControls/items/text,rows/items/cells/items/value,rows/items/cells/items/cellIndex,rows/items/cells/items/value,rows/items/cells/items/body/font');
await context.sync();

var rows = table.rows;
let cell = row.cells.items[index];
let cellBody = cell.body;
cellBody.clear();
cellBody.insertText(data.CellDisplayValue.trim(), "Replace");
cellBody.insertContentControl();
await context.sync();

Before
image
After
image

@microsoft-github-policy-service microsoft-github-policy-service bot added the Needs: triage 🔍 New issue, needs PM on rotation to triage ASAP label Dec 23, 2024
@RuizhiSunMS RuizhiSunMS added Needs: author feedback Waiting for author (creator) of Issue to provide more info Area: Word Issue related to Word add-ins and removed Needs: triage 🔍 New issue, needs PM on rotation to triage ASAP labels Dec 23, 2024
@RuizhiSunMS RuizhiSunMS self-assigned this Dec 23, 2024
@RuizhiSunMS
Copy link
Contributor

Hi @Raj-Dev1, thx for reaching out here.
Would you please confirm such things:

  1. share your doc here
  2. how does it work on web?

@Raj-Dev1
Copy link
Author

Hi @RuizhiSunMS,

  1. Below is the provided document:
    Table Formatting Issue.docx
  2. We are developing a desktop application.

Let me know if you need any further information.

@microsoft-github-policy-service microsoft-github-policy-service bot added Needs: attention 👋 Waiting on Microsoft to provide feedback and removed Needs: author feedback Waiting for author (creator) of Issue to provide more info labels Dec 24, 2024
@RuizhiSunMS
Copy link
Contributor

Hi @Raj-Dev1, I did another try when waiting for experts' investigation and found something new.
Here looks we have 2 ways to change that text, one is what you did to add a new content control, another is change the current content control's text. If you use the second, the style is reserved.

async function run() {
  await Word.run(async (context) => {
    // const body = context.document.body;

    // console.log("Your code goes here");
    // let res = body.getContentControls();
    // res.load("items");
    // await context.sync();
    // console.log(res.items.length);
    // console.log(res);
    const tartag = "C_165689899e4b4b3b9917a3cb90cbd30a";
    const docCC = context.document.contentControls;
    docCC.load("items");
    await context.sync();

    var findTbl = docCC.items.filter(a => a.tag == tartag);
    var table = findTbl[0].parentTable;
    context.load(table, 'rows/items/rowIndex,rowCount,rows/items/cells/items/body/font/superscript,rows/items/cells/items/body/contentControls/items/tag,rows/items/cells/items/body/contentControls/items/text,rows/items/cells/items/value,rows/items/cells/items/cellIndex,rows/items/cells/items/value,rows/items/cells/items/body/font');
    await context.sync();
    // console.log(findTbl);
    var row = table.rows.items[2];
    // rows.load();
    // await context.sync();
    // console.log(rows);
    row.load("cells,cells/items");
    let cell = row.cells.items[4];
    let cellBody = cell.body;
    cellBody.clear();
    cellBody.insertText("123", "Replace");
    cellBody.insertContentControl();
    await context.sync();
    var findTbl = docCC.items.filter(a => a.tag == tartag);
    let one = findTbl[0];
    one.insertText("0","Replace");
    await context.sync();
  });
}

@RuizhiSunMS RuizhiSunMS added Needs: author feedback Waiting for author (creator) of Issue to provide more info and removed Needs: attention 👋 Waiting on Microsoft to provide feedback labels Dec 24, 2024
@Raj-Dev1
Copy link
Author

Hi @RuizhiSunMS,
Thank you for suggesting an alternative approach, but my requirement is to remove the old content control and add a new one. I need to apply content controls only to numeric values (ignoring currency symbols, minus signs, and parentheses, etc.) and, for non-numeric values, set the content control on the entire value. Additionally, I need to update multiple values in the table based on changes while retaining the original styles. I hope this clarifies my requirement.

@microsoft-github-policy-service microsoft-github-policy-service bot added Needs: attention 👋 Waiting on Microsoft to provide feedback and removed Needs: author feedback Waiting for author (creator) of Issue to provide more info labels Dec 24, 2024
@RuizhiSunMS
Copy link
Contributor

@Raj-Dev1, sorry that I don't get your point very well.
Do you mean you need to do different actions on content controls with numeric values and non-numeric values?
I still don't understand the necessity to have new content controls.
Anyway, if you keep your idea, please be patient. Considering the vacation is near, response may be late.

@RuizhiSunMS RuizhiSunMS added Needs: author feedback Waiting for author (creator) of Issue to provide more info and removed Needs: attention 👋 Waiting on Microsoft to provide feedback labels Dec 24, 2024
@Raj-Dev1
Copy link
Author

@Raj-Dev1, sorry that I don't get your point very well. Do you mean you need to do different actions on content controls with numeric values and non-numeric values? I still don't understand the necessity to have new content controls. Anyway, if you keep your idea, please be patient. Considering the vacation is near, response may be late.

@RuizhiSunMS, we need to perform different actions on content controls based on whether they contain numeric or non-numeric values. Since there are multiple scenarios, we need to insert new content controls accordingly.

Example:
image

@microsoft-github-policy-service microsoft-github-policy-service bot added Needs: attention 👋 Waiting on Microsoft to provide feedback and removed Needs: author feedback Waiting for author (creator) of Issue to provide more info labels Dec 26, 2024
@RuizhiSunMS
Copy link
Contributor

Content control also has .search. If one identifies the type, one can sure manipulate the value instead of replacing the whole content control, even though there may be several types.
Our experts are taking their vacation. I want to do this technical discussion in case that this behavior is by-designed.

@RuizhiSunMS RuizhiSunMS added Needs: author feedback Waiting for author (creator) of Issue to provide more info and removed Needs: attention 👋 Waiting on Microsoft to provide feedback labels Dec 26, 2024
@RuizhiSunMS
Copy link
Contributor

Our experts did some investigations and found that the added string's style is just the default style of this document. This doc's default style is the 'A1'.
image
When cursor points to a cell's content control. the chosen style is 'A1'. When move to an empty one, the style is 'Table Paragraph'
I checked with the shared doc and confirmed this. Looks there are something wrong when you set the style rather than an API case.
image
Back to ways to resolve this, our suggestions are:

  1. just change the value but not to replace content control
  2. set style again after all replacement works are completed

@Raj-Dev1
Copy link
Author

@RuizhiSunMS How can I retrieve the original style and apply it to a new content control or cell body?

@microsoft-github-policy-service microsoft-github-policy-service bot added Needs: attention 👋 Waiting on Microsoft to provide feedback and removed Needs: author feedback Waiting for author (creator) of Issue to provide more info labels Dec 30, 2024
@RuizhiSunMS
Copy link
Contributor

@RuizhiSunMS How can I retrieve the original style and apply it to a new content control or cell body?

You may can try like:

  1. create a customary style named "StyleNameA" for content control and call contentcontrol.set({style:"StyleNameA"})
  2. manually set font like
    let cc = cellBody.insertContentControl(); cc.font.name = "Calibri"; cc.font.size = 8; await context.sync();

@RuizhiSunMS RuizhiSunMS removed the Needs: attention 👋 Waiting on Microsoft to provide feedback label Dec 30, 2024
@RuizhiSunMS RuizhiSunMS added the Needs: author feedback Waiting for author (creator) of Issue to provide more info label Dec 30, 2024
@guoms1 guoms1 self-assigned this Jan 3, 2025
Copy link
Contributor

This issue has been automatically marked as stale because it is marked as needing author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. Thank you for your interest in Office Add-ins!

@microsoft-github-policy-service microsoft-github-policy-service bot added the Status: no recent activity Issue or PR is stale (no recent activity) label Jan 3, 2025
Copy link
Contributor

This issue has been closed due to inactivity. Please comment if you still need assistance and we'll re-open the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Word Issue related to Word add-ins Needs: author feedback Waiting for author (creator) of Issue to provide more info Status: no recent activity Issue or PR is stale (no recent activity)
Projects
None yet
Development

No branches or pull requests

3 participants