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

update the table structure #4531

Closed
charangoldenhills opened this issue Apr 23, 2024 · 3 comments
Closed

update the table structure #4531

charangoldenhills opened this issue Apr 23, 2024 · 3 comments
Assignees
Labels
Area: Excel Feedback on Excel content 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

@charangoldenhills
Copy link

i have created a table by using the js api
code is
create: async (
columns: string[],
data: Record<string, TableCell>[],
metadata?: { [key: string]: any },
// range: number[] = []
) => {
return Excel.run(async context => {
const sheet = context.workbook.worksheets.getActiveWorksheet();
const selectedRange = context.workbook.getSelectedRange();
selectedRange.load('address');
await context.sync();
const userSelectedCell = splitCharAndNumber(selectedRange.address.split('!')[1]);
let cellRowPoint = userSelectedCell[1];
let cellColumnPoint = charToNumber(userSelectedCell[0]);

    const currentCell = sheet.getCell(cellRowPoint, cellColumnPoint);
    const tableRange = await getTableRange(context, columns, cellRowPoint, cellColumnPoint);
    console.log(tableRange, '--------------------------------------- tabel range');
    // Sheet1!F11:Sheet1!H11 ---------------------------------------
    const table: Excel.Table = currentCell.worksheet.tables.add(tableRange, true);
    const formattedColumns = columns.map(column => formatItemName(column));
    console.log('format data', formattedColumns, 'tabel', table);
    table.getHeaderRowRange().values = [formattedColumns];
    table.load('name');
    const rows = data.map(item => columns.map(key => item[key]));
    rows.length > 0 && table.rows.add(0, rows);
    await context.sync();
    const settings = context.workbook.settings;
    if(metadata){
      metadata.filters.tableName = table.name;
    }
    settings.add(table.name, metadata);
    await context.sync();
    console.log("creating the tabel set ",table)
    return table;
  });
},
--------------------------------------
i want to update the table 
  1. update the range according to the new data set
    2.columns should be loaded according to the new data
  2. existing table name should be present and data should be new data along with the range i.e, structure is new

updateTable: async (
newColumns: string[],
newData: Record<string, TableCell>[],
tableName: string,
metadata?: { [key: string]: any },
) => {
return Excel.run(async context => {
const sheet = context.workbook.worksheets.getActiveWorksheet();

    // Find the table by name
    const table = sheet.tables.getItem(tableName);

    await context.sync();
    table.load("name");

    // Clear existing data and format
    table.getDataBodyRange().clear();
    table.getHeaderRowRange().clear();
    // table.getRange().clear();
    console.log("Clearing the existing data");

    // Set new column headers
    const formattedColumns = newColumns.map(column => formatItemName(column));
    table.getHeaderRowRange().values = [formattedColumns];
    await context.sync();


   

   

    // Add new data to the existing table range
    const rows = newData.map(item => newColumns.map(key => item[key]));
    if (rows.length > 0) {
      table.rows.add(0, rows); // Add new rows to the existing table
    }

    // Update metadata if provided
    const settings = context.workbook.settings;
    if (metadata) {
      metadata.filters = { tableName }; // Set the table name in the metadata
      settings.add(tableName, metadata);
    }

    await context.sync();

    return table;
  });
}
----------------------------------------------
please suggest the answer for this.....
@microsoft-github-policy-service microsoft-github-policy-service bot added the Needs: triage 🔍 New issue, needs PM on rotation to triage ASAP label Apr 23, 2024
@davidchesnut
Copy link
Member

Hi @charangoldenhills, can you please provide more information about what is going wrong? Did the table not get created properly? Is it not updating properly? Also if you can provide which platform you are on (Mac, browser, Windows)? Thanks!

@davidchesnut davidchesnut added Area: Excel Feedback on Excel content Needs: author feedback Waiting for author (creator) of Issue to provide more info and removed Needs: triage 🔍 New issue, needs PM on rotation to triage ASAP labels Apr 23, 2024
@davidchesnut davidchesnut self-assigned this Apr 23, 2024
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 Apr 27, 2024
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: Excel Feedback on Excel content 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

2 participants