Excel Office Script未清除单元格。出现”检测到不可访问的代码(7027)”错误。

huangapple go评论62阅读模式
英文:

Excel Office Script not clearing cells. Getting unreachable code detected (7027) error

问题

这个脚本的问题是在清除"B4:B120"区域的数值时出现了问题,可能是因为该部分被注释掉了。以下是你需要关注的代码部分:

  // Clear the "Margin Updates" column.
  const targetSheet = workbook.getActiveWorksheet();
  const getRange = targetSheet.getRange("B4:B120");
  getRange.clear(ExcelScript.ClearApplyTo.contents);

你可以尝试取消注释这部分代码,以便在执行脚本时清除"B4:B120"区域的内容。这应该解决你的问题。

英文:

I have this script which works all except for the clearing of the B4:B120 area "// Clear the "Margin Updates" column." section which is greyed out for some reason):

function main(workbook: ExcelScript.Workbook): ReportImages {
  // Recalculate the workbook to ensure all tables and charts are updated.
  workbook.getApplication().calculate(ExcelScript.CalculationType.full);

  // Get the data from the "Target Margins - FHM" table. (name of Excel tab, not name of table)
  let sheet1 = workbook.getWorksheet("Sheet1");
  const table = workbook.getWorksheet('Target Margins - FHM').getTables()[0];
  const rows = table.getRange().getTexts();

  // Get only the Product Type and "Margin Update" columns, then remove the "Total" row.
  const selectColumns = rows.map((row) => {
    return [row[0], row[1]];
  });

  // Delete the "ChartSheet" worksheet if it's present, then recreate it.
  workbook.getWorksheet('ChartSheet')?.delete();
  const chartSheet = workbook.addWorksheet('ChartSheet');

  // Add the selected data to the new worksheet.
  const targetRange = chartSheet.getRange('A1').getResizedRange(selectColumns.length - 1, selectColumns[0].length - 1);
  targetRange.setValues(selectColumns);

   // Get images of the chart and table, then return them for a Power Automate flow.
  const tableImage = table.getRange().getImage();
  return { tableImage };

  // Clear the "Margin Updates" column.
  const targetSheet = workbook.getActiveWorksheet();
  const getRange = targetSheet.getRange("B4:B120");
  getRange.clear(ExcelScript.ClearApplyTo.contents);`

}

// The interface for table and chart images.
interface ReportImages {
  tableImage: string
}

The code copies the data in sections of the A and B columns (which constitute a table) and sends an email via Power Automate flow. Unfortunately, I need the section of the B column to be clear of values (not formatting or style) after which this flow is not doing.

I'd greatly appreciate help with this problem.

Thank you.

@cybernetic. nomad:
When I try using Range ("B4:B120").Clear I receive
>unreachable code detected (7027)

and

>and "cannot find name 'Range' (2304)

Office Script Range Clear Error

答案1

得分: 1

在JavaScript中,一旦return关键字被评估,函数就会立即退出。这就是为什么它说你的代码是不可访问的。因此,你需要重构你的代码,使返回操作发生在最后。所以你可以更新你的代码,看起来像这样:

// 清除“Margin Updates”列。
const targetSheet = workbook.getActiveWorksheet();
const getRange = targetSheet.getRange("B4:B120");
getRange.clear(ExcelScript.ClearApplyTo.contents);

return { tableImage };
英文:

In JavaScript, the function exits as soon as the return keyword is evaluated. That's why it's saying your code is unreachable. So you have to restructure your code so that the return happens at the end. So you can update your code to look something like this:

    // Clear the "Margin Updates" column.
    const targetSheet = workbook.getActiveWorksheet();
    const getRange = targetSheet.getRange("B4:B120");
    getRange.clear(ExcelScript.ClearApplyTo.contents);
    
    return { tableImage };

huangapple
  • 本文由 发表于 2023年2月14日 06:08:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441672.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定