You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Office JS插件UI展示Excel工作表列表?工具选型咨询

Solution for Your Office JS Add-in Questions

1. Extending the "List worksheets" Script Lab Example to Display in Taskpanel (React/TypeScript)

Let's modify the default Script Lab example to render the worksheet list directly in the task pane instead of logging to the console. Here's a complete React/TypeScript implementation you can paste into Script Lab:

import * as React from "react";
import * as ReactDOM from "react-dom";

interface WorksheetItem {
  id: string;
  name: string;
}

const App = () => {
  const [worksheets, setWorksheets] = React.useState<WorksheetItem[]>([]);
  const [isLoading, setIsLoading] = React.useState(false);

  const fetchWorksheets = async () => {
    setIsLoading(true);
    try {
      await Excel.run(async (context) => {
        const sheets = context.workbook.worksheets.load("id,name");
        await context.sync();
        
        const sheetList = sheets.items.map(sheet => ({
          id: sheet.id,
          name: sheet.name
        }));
        setWorksheets(sheetList);
      });
    } catch (error) {
      console.error("Error fetching worksheets:", error);
      // Optional: Add error message display in UI if needed
    } finally {
      setIsLoading(false);
    }
  };

  React.useEffect(() => {
    // Fetch worksheets automatically when the component loads
    fetchWorksheets();
  }, []);

  return (
    <div style={{ padding: "1rem", fontFamily: "Segoe UI" }}>
      <h3>Worksheet List</h3>
      {isLoading ? (
        <p>Loading worksheets...</p>
      ) : (
        <ul style={{ listStyleType: "disc", paddingLeft: "1.5rem" }}>
          {worksheets.map(sheet => (
            <li key={sheet.id}>{sheet.name}</li>
          ))}
        </ul>
      )}
      <button 
        onClick={fetchWorksheets} 
        style={{ marginTop: "1rem", padding: "0.5rem 1rem" }}
      >
        Refresh List
      </button>
    </div>
  );
};

ReactDOM.render(<App />, document.getElementById("container"));

Key Implementation Details:

  • Uses React's useState hook to manage the worksheet list and loading state for a smooth UI experience
  • Wraps the API call in Excel.run() to handle context synchronization and error management correctly
  • Maps the raw worksheet objects from the Office API to a simplified array for easy rendering
  • Includes automatic initial data fetching and a manual refresh button for flexibility

To use this in Script Lab:

  1. Create a new snippet and set the editor to React + TypeScript
  2. Replace the default code with the snippet above
  3. Click "Run" – the worksheet list will appear directly in the task pane

2. Choosing Between Script Lab and Formal Office Add-in for Your Migration Project

For your use case (migrating a Google Apps Script project to Excel, only for a specific workbook, no public distribution), Script Lab is the clear better choice – here's why:

Script Lab Advantages for Your Scenario:

  • No Node server required: You don't need to run or host a separate backend; everything runs directly within Excel's task pane.
  • Instant iteration: Edit code and see changes immediately without reloading or re-sideloading the add-in.
  • Tied to specific workbooks: Save the Script Lab snippet directly to your target workbook (via the "Save to Workbook" option) so it's always available when that file is opened.
  • Full API access: Script Lab supports all the same Excel JS APIs as formal add-ins, so you won't hit functionality limits.

When Would a Formal Add-in Make Sense?

Only consider a formal add-in if:

  • You need to distribute the tool to multiple users across your organization (and want centralized deployment via the Microsoft 365 admin center)
  • You require complex external dependencies (like niche npm packages not available in Script Lab)
  • You need persistent storage outside the workbook (e.g., cloud databases, cross-workbook user settings)

Is Script Lab Suitable for Non-Prototyping Use?

Absolutely. While it's marketed as a prototyping tool, Script Lab is stable enough for internal, single-workbook use cases. Many teams rely on it to build custom, workbook-specific tools without ever moving to a formal add-in. The only real limitation is that you can't package it for public distribution – which doesn't matter for your project.

内容的提问来源于stack exchange,提问作者deinspanjer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:46:53