Sheet 命名 office js
Sheet names office js
我试图在外部 Excel 文件中接收 sheet 名称,但我只得到一个空数组。
async function insertSheets() {
const myFile = <HTMLInputElement>document.getElementById("file");
const reader = new FileReader();
reader.onload = (event) => {
Excel.run(async (context) => {
// Remove the metadata before the base64-encoded string.
const startIndex = reader.result.toString().indexOf("base64,");
const workbookContents = reader.result.toString().substr(startIndex + 7);
// Retrieve the workbook.
const workbook = context.workbook;
// Set up the insert options.
var options = {
sheetNamesToInsert: [], // Insert all the worksheets from the source workbook.
positionType: Excel.WorksheetPositionType.after, // Insert after the `relativeTo` sheet.
relativeTo: "Sheet1"
}; // The sheet relative to which the other worksheets will be inserted. Used with `positionType`.
// Insert the workbook.
//workbook.insertWorksheetsFromBase64(workbookContents, options);
var names = context.workbook.names;
names.load();
await context.sync();
console.log(JSON.stringify(workbook.names))
return context.sync();
});
};
// Read the file as a data URL so that we can parse the base64-encoded string.
reader.readAsDataURL(myFile.files[0]);
}
代码主要来自office-js示例代码。有什么想法可以检索 sheet 名称吗?
您可以使用项目 属性 获得作品集 sheets。加载后,您可以遍历项目以获取所有 sheet 名称并将它们添加到数组中。请看下面的代码:
const wb: Excel.Workbook = context.workbook
const wbWorksheets: Excel.WorksheetCollection = wb.worksheets
wbWorksheets.load("items")
await context.sync()
let wsItems: Excel.Worksheet[] = wbWorksheets.items
let newArr: string[] = []
wsItems.forEach(n=>newArr.push(n.name))
newArr.forEach(n=>console.log(n))
您可以尝试以下脚本:
async function insertSheets() {
const myFile = <HTMLInputElement>document.getElementById("file");
const reader = new FileReader();
reader.onload = (event) => {
Excel.run(async (context) => {
// Remove the metadata before the base64-encoded string.
const startIndex = reader.result.toString().indexOf("base64,");
const workbookContents = reader.result.toString().substr(startIndex + 7);
// Retrieve the workbook.
const workbook = context.workbook;
// Set up the insert options.
const options = {
sheetNamesToInsert: [], // Insert all the worksheets from the source workbook.
positionType: Excel.WorksheetPositionType.after, // Insert after the `relativeTo` sheet.
relativeTo: "Sheet1"
}; // The sheet relative to which the other worksheets will be inserted. Used with `positionType`.
// Insert the workbook.
const sheetIds = workbook.insertWorksheetsFromBase64(workbookContents, options);
// have to sync to get the sheetIds loaded
await context.sync();
// get the worksheet references by the returned sheetIds
const worksheets = sheetIds.value.map(uuid => context.workbook.worksheets.getItem(uuid));
for (const w of worksheets) {
w.load('name');
}
await context.sync();
const names = worksheets.map(w => w.name); // this is the name array of inserted worksheets
console.log(names);
});
};
// Read the file as a data URL so that we can parse the base64-encoded string.
reader.readAsDataURL(myFile.files[0]);
}
它应该打印刚刚插入的工作表名称。
我试图在外部 Excel 文件中接收 sheet 名称,但我只得到一个空数组。
async function insertSheets() {
const myFile = <HTMLInputElement>document.getElementById("file");
const reader = new FileReader();
reader.onload = (event) => {
Excel.run(async (context) => {
// Remove the metadata before the base64-encoded string.
const startIndex = reader.result.toString().indexOf("base64,");
const workbookContents = reader.result.toString().substr(startIndex + 7);
// Retrieve the workbook.
const workbook = context.workbook;
// Set up the insert options.
var options = {
sheetNamesToInsert: [], // Insert all the worksheets from the source workbook.
positionType: Excel.WorksheetPositionType.after, // Insert after the `relativeTo` sheet.
relativeTo: "Sheet1"
}; // The sheet relative to which the other worksheets will be inserted. Used with `positionType`.
// Insert the workbook.
//workbook.insertWorksheetsFromBase64(workbookContents, options);
var names = context.workbook.names;
names.load();
await context.sync();
console.log(JSON.stringify(workbook.names))
return context.sync();
});
};
// Read the file as a data URL so that we can parse the base64-encoded string.
reader.readAsDataURL(myFile.files[0]);
}
代码主要来自office-js示例代码。有什么想法可以检索 sheet 名称吗?
您可以使用项目 属性 获得作品集 sheets。加载后,您可以遍历项目以获取所有 sheet 名称并将它们添加到数组中。请看下面的代码:
const wb: Excel.Workbook = context.workbook
const wbWorksheets: Excel.WorksheetCollection = wb.worksheets
wbWorksheets.load("items")
await context.sync()
let wsItems: Excel.Worksheet[] = wbWorksheets.items
let newArr: string[] = []
wsItems.forEach(n=>newArr.push(n.name))
newArr.forEach(n=>console.log(n))
您可以尝试以下脚本:
async function insertSheets() {
const myFile = <HTMLInputElement>document.getElementById("file");
const reader = new FileReader();
reader.onload = (event) => {
Excel.run(async (context) => {
// Remove the metadata before the base64-encoded string.
const startIndex = reader.result.toString().indexOf("base64,");
const workbookContents = reader.result.toString().substr(startIndex + 7);
// Retrieve the workbook.
const workbook = context.workbook;
// Set up the insert options.
const options = {
sheetNamesToInsert: [], // Insert all the worksheets from the source workbook.
positionType: Excel.WorksheetPositionType.after, // Insert after the `relativeTo` sheet.
relativeTo: "Sheet1"
}; // The sheet relative to which the other worksheets will be inserted. Used with `positionType`.
// Insert the workbook.
const sheetIds = workbook.insertWorksheetsFromBase64(workbookContents, options);
// have to sync to get the sheetIds loaded
await context.sync();
// get the worksheet references by the returned sheetIds
const worksheets = sheetIds.value.map(uuid => context.workbook.worksheets.getItem(uuid));
for (const w of worksheets) {
w.load('name');
}
await context.sync();
const names = worksheets.map(w => w.name); // this is the name array of inserted worksheets
console.log(names);
});
};
// Read the file as a data URL so that we can parse the base64-encoded string.
reader.readAsDataURL(myFile.files[0]);
}
它应该打印刚刚插入的工作表名称。