office-scripts - 将表格从一张纸复制到另一张纸

标签 office-scripts

我想将整个表格从一张纸(inputSheet)复制到另一张纸(outputSheet),然后继续复制并添加更多表格到同一个ouputSheet

以下是我尝试过的一些方法:

function main(workbook: ExcelScript.Workbook) {
  const inputSheet = workbook.getWorksheet("Input")
  const outputSheet = workbook.getWorksheet("Output")


  // Worksheet addTable: The argument is invalid or missing or has an incorrect format.
  const tableRange = inputSheet.getTables()[0].convertToRange()
  const table = outputSheet.addTable(tableRange, true)

 
  // Worksheet addTable: The argument is invalid or missing or has an incorrect format.
  const tablaRange2 = inputSheet.getTables()[0].getRange()
  const tabla2 = outputSheet.addTable(tablaRange2, true)


  // this one kind of work but :
  //   1- have to know the range in advance
  //   2- the second table I copy(to the same outputSheet), gives an error saying that headers can't have rich text. 
  const newRange = outputSheet.getRange("A1:Z20")
    .copyFrom(inputSheet.getRange("A1:Z20"), ExcelScript.RangeCopyType.all, false, false);
  const newTable = eneSheet.addTable("A1:Z20", false)
}

我希望有一些类似于复制工作表的简单方法:

  const newSheet = previousSheet.copy(ExcelScript.WorksheetPositionType.after, anotherSheet)

但还没有找到可以做到这一点的表格或工作表方法。

最佳答案

如果您正在处理包含相似结构的表,则需要编写一些逻辑来组合它们。没有 API 可以创建表的新副本。添加来自其他类似表的行 - 这是一个非常自定义的逻辑。

在此处查看此项目以了解逻辑的两种变体(因为我不确定哪一种适合您的需求)。请观看分步视频以了解其工作原理。如果这不是您想要的场景,请告诉我。

https://github.com/sumurthy/officescripts-projects/tree/main/Copy%20Tables%20to%20Master%20Table

关于office-scripts - 将表格从一张纸复制到另一张纸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64883098/

相关文章:

javascript - Office Scripts Excel - 为气泡图标记设置透明度

typescript - 数组中的自定义日期格式值

excel - Office 脚本,忽略删除范围名称,没有错误

excel - Office 脚本错误 - 属性不可用

Excel Office 脚本 - 将 "blank cells"替换为文本 "null"

excel - 将 Excel 脚本嵌入到 .XLSX 文件中?

office-scripts - 如何在Office脚本中调用另一个脚本?

excel - 是否可以在办公脚本中将数据从一张表复制、粘贴和删除到另一张表?

excel - 如何编写执行预定义查询以将数据加载到工作表中的办公脚本?