使用 Excel JavaScript API 撤消支持

Excel 加载项支持撤消行为。 这将保留 Excel JavaScript API 执行的作和用户在 Excel 中执行的作。 这些作将保存在单个用户的 撤消堆栈 中,允许用户在需要时回退其作。

撤消分组

Excel JavaScript API 还支持撤消分组。 这样,就可以将多个 API 调用分组到外接程序用户的单个可撤消作中。 例如,如果外接程序需要跨多个工作表进行多个不同的更新以响应单个用户命令,则可以将所有这些更新包装在一个组中。 这是通过 mergeUndoGroup 提供给 函数的 属性完成的 Excel.run

如果组中的 API 不提供撤消支持,则会 UndoNotSupported 引发错误,告知你无法对作进行分组。 加载项应正常处理此错误,并向用户显示一条合理的消息。

以下代码示例演示如何合并设置为 truemergeUndoGroup多个作。

重要

确保所有分组 API 调用都支持撤消以避免错误。 有关详细信息 ,请参阅不支持的 API

await Excel.run({ mergeUndoGroup: true }, async (context) => { 
    const sheet = context.workbook.worksheets.getActiveWorksheet(); 
    let range = sheet.getRange("A1"); 
    range.values = [["123"]]; 
    
    await context.sync(); 
    
    range = sheet.getRange("B2"); 
    range.values = [["456"]];

    await context.sync(); 
}); 

不支持的 API

大多数 Excel JavaScript API 都支持撤消作。 但是,请参阅下表,了解不支持撤消行为的 API 列表。

提示

如果在外接程序中调用不受支持的 API,则会从该 API 调用开始清除用户的撤消堆栈,并且用户无法撤消超过该点的作。

API 在 Excel web 版 中受支持 在 Excel on Windows 和 Excel on Mac 中受支持 注意
AllowEditRange.address
AllowEditRange.delete
AllowEditRange.pauseProtection
AllowEditRange.setPassword
AllowEditRange.title
AllowEditRangeCollection.add
AllowEditRangeCollection.pauseProtection
Chart.categoryLabelLevel
Chart.seriesNameLevel
ChartPivotOptions.showAxisFieldButtons
ChartPivotOptions.showLegendFieldButtons
ChartPivotOptions.showReportFilterFieldButtons
ChartPivotOptions.showValueFieldButtons
ChartTrendlineLabel.formula
DataConnectionCollection.refreshAll
DocumentProperties.author​
DocumentProperties.category
DocumentProperties.comments
DocumentProperties.company
DocumentProperties.keywords
DocumentProperties.manager
DocumentProperties.revisionNumber
DocumentProperties.subject
DocumentProperties.title
LinkedWorkbook.refresh
LinkedWorkbookCollection.refreshAll
NamedItem.comment
PivotTableStyle.delete API 不支持 在 Windows 和 Mac 上的 Excel 中共同创作撤消作。
PivotTableStyle.duplicate
PivotTableStyle.name
PivotTableStyleCollection.add API 不支持 在 Windows 和 Mac 上的 Excel 中共同创作撤消作。
PivotTableStyleCollection.setDefault API 不支持 在 Windows 和 Mac 上的 Excel 中共同创作撤消作。
Query.delete API 支持在 Windows 和 Mac 上的 Excel 中撤消作,但不支持重做。
Query.refresh API 支持在 Windows 和 Mac 上撤消 Excel,但不支持重做。
QueryCollection.refreshAll API 支持在 Windows 和 Mac 上撤消 Excel,但不支持重做。
Slicer.name
Slicer.nameInFormula
SlicerStyle.delete API 不支持 在 Windows 和 Mac 上的 Excel 中共同创作撤消作。
SlicerStyle.duplicate
SlicerStyle.name
SlicerStyleCollection.add API 不支持 在 Windows 和 Mac 上的 Excel 中共同创作撤消作。
SlicerStyleCollection.setDefault API 不支持 在 Windows 和 Mac 上的 Excel 中共同创作撤消作。
Style.addIndent
Style.autoIndent
Style.formulaHidden
Style.horizontalAlignment
Style.includeAlignment
Style.includeBorder
Style.includeFont
Style.includeNumber
Style.includePatterns
Style.includeProtection
Style.indentLevel
Style.locked
Style.numberFormat
Style.numberFormatLocal
Style.orientation
Style.readingOrder
Style.shrinkToFit
Style.textOrientation
Style.verticalAlignment
Style.wrapText
TableStyle.delete API 不支持 在 Windows 和 Mac 上的 Excel 中共同创作撤消作。
TableStyle.duplicate
TableStyle.name
TableStyleCollection.add API 不支持 在 Windows 和 Mac 上的 Excel 中共同创作撤消作。
TableStyleCollection.setDefault API 不支持 在 Windows 和 Mac 上的 Excel 中共同创作撤消作。
TimelineStyle.delete API 不支持 在 Windows 和 Mac 上的 Excel 中共同创作撤消作。
TimelineStyle.duplicate
TimelineStyle.name
TimelineStyleCollection.add API 不支持 在 Windows 和 Mac 上的 Excel 中共同创作撤消作。
TimelineStyleCollection.setDefault API 不支持 在 Windows 和 Mac 上的 Excel 中共同创作撤消作。
Workbook.close
Workbook.insertWorksheetsFromBase64
Workbook.save
WorkbookProtection.protect
WorkbookProtection.unprotect
Worksheet.copy
Worksheet.delete
Worksheet.name
Worksheet.standardWidth
Worksheet.position
Worksheet.visibility​
WorksheetCollection.addFromBase64
WorksheetProtection.pauseProtection
WorksheetProtection.protect
WorksheetProtection.resumeProtection
WorksheetProtection.setPassword
WorksheetProtection.unprotect
WorksheetProtection.updateOptions

另请参阅