继续我的最后一个问题,我已经将数据从工作表转换为表,但我遇到了另一个问题。
我有两个脚本,称它们为“convert”和“combine”。如果我把它们分开运行,它们就会工作。我要么把它们放在同一个脚本中,要么在Power Automate流中一起运行,这是我的期望。我在第17行出错
我们无法运行脚本。请再试一次。
Office JS错误:第17行:范围设置值:表格中的列和冒号的名称与瘟疫的尾部和尺寸相对应。“
尝试禁用第17行“//targetRange.setValues(headerValues);”,然后在第31行出现错误
“我们无法运行脚本。请重试。
Office JS错误:第31行:表格添加行:表格中的列和冒号的名称与瘟疫的尾部和尺寸相对应。“
冲突在哪里?
转换
function main(workbook: ExcelScript.Workbook) {
let worksheets = workbook.getWorksheets();
for (let i = 1; i < worksheets.length; i++) {
let selectedSheet = workbook.getWorksheets()[i];
selectedSheet.getRange("1:1").delete(ExcelScript.DeleteShiftDirection.up);
let newTable = workbook.addTable(selectedSheet.getRange("A1:AJ2"), true);
}
workbook.getWorksheet('Feuil1')?.delete();
}
结合
function main(workbook: ExcelScript.Workbook) {
// Delete the "Combined" worksheet, if it's present.
workbook.getWorksheet('Combined')?.delete();
workbook.getWorksheet('Feuil1')?.delete();
// Create a new worksheet named "Combined" for the combined table.
const newSheet = workbook.addWorksheet('Combined');
// Get the header values for the first table in the workbook.
// This also saves the table list before we add the new, combined table.
const tables = workbook.getTables();
const headerValues = tables[1].getHeaderRowRange().getTexts();
console.log(headerValues);
// Copy the headers on a new worksheet to an equal-sized range.
const targetRange = newSheet.getRange('A1').getResizedRange(headerValues.length-1, headerValues[0].length-1);
//targetRange.setValues(headerValues);
// Add the data from each table in the workbook to the new table.
const combinedTable = newSheet.addTable(targetRange.getAddress(), true);
for (let table of tables) {
let dataValues = table.getRangeBetweenHeaderAndTotal().getTexts();
let rowCount = table.getRowCount();
// If the table is not empty, add its rows to the combined table.
if (rowCount > 0) {
combinedTable.addRows(0, dataValues);
}
}
}