如何通过编写脚本为谷歌表中表格的两个特定行绘制折线图?

问题描述 投票:0回答:1

我有一个表格,其中包含一些参数,它们的值将在 23 天内完成。 我需要 row3(Comp Temp)和 row6(Air Meas)值的折线图。

我的问题是,一切都很好,但是如果我为第 4 行插入值,第 3 行的图表将会混乱,如果我为第 7 行插入值,第 6 行的图表也会发生这种情况!!

我在 Google 扩展表中尝试了这个脚本:

function createCompTempLineChart(sheet) {
  // Define the range for Comp Temp (Row 3), starting at Column B (Column 2)
  var startColumn = 2; // Column B
  var lastColumn = sheet.getLastColumn(); // Find the last column with data

  // Set empty or zero values in other rows to prevent chart from considering them
  for (var col = startColumn; col <= lastColumn; col++) {
    sheet.getRange(4, col).setValue(''); // Empty Row 4
    sheet.getRange(5, col).setValue(''); // Empty Row 5
    // You can set additional rows to '' or 0 if needed to ensure no data influences the chart
  }

  // Explicitly restrict the range to Row 3 only (ignore other rows)
  var compTempRange = sheet.getRange(3, startColumn, 1, lastColumn - startColumn + 1); // Only Row 3

  // Create or update the Comp Temp chart
  var chart = sheet.newChart()
    .setChartType(Charts.ChartType.LINE) // Set chart type to Line Chart
    .addRange(compTempRange) // Only use Row 3 data
    .setTransposeRowsAndColumns(true) // Treat Row 3 as series
    .setPosition(30, 5, 0, 0) // Adjust position for the chart
    .setOption('title', 'Comp Temp (Row 3)') // Title for the Comp Temp chart
    .build();

  sheet.insertChart(chart); // Insert the Comp Temp chart
}

function createAirMeasLineChart(sheet) {
  // Define the range for Air Meas (Row 6), starting at Column B (Column 2)
  var startColumn = 2; // Column B
  var lastColumn = sheet.getLastColumn(); // Find the last column with data

  // Set empty or zero values in other rows to prevent chart from considering them
  for (var col = startColumn; col <= lastColumn; col++) {
    sheet.getRange(3, col).setValue(''); // Empty Row 3 to prevent influence in this chart
    sheet.getRange(4, col).setValue(''); // Empty Row 4
    sheet.getRange(5, col).setValue(''); // Empty Row 5
    // You can set additional rows to '' or 0 if needed
  }

  // Explicitly restrict the range to Row 6 only (ignore other rows)
  var airMeasRange = sheet.getRange(6, startColumn, 1, lastColumn - startColumn + 1); // Only Row 6

  // Create or update the Air Meas chart
  var chart = sheet.newChart()
    .setChartType(Charts.ChartType.LINE) // Set chart type to Line Chart
    .addRange(airMeasRange) // Only use Row 6 data
    .setTransposeRowsAndColumns(true) // Treat Row 6 as series
    .setPosition(50, 5, 0, 0) // Adjust position for the chart
    .setOption('title', 'Air Meas (Row 6)') // Title for the Air Meas chart
    .build();

  sheet.insertChart(chart); // Insert the Air Meas chart
}
javascript google-sheets plot charts linechart
1个回答
0
投票

您的问题是当您添加数据时图表会选取其他行。只需将图表范围设置为您想要的行,而不是清空其他行。

试试这个:

对于第 3 行图表:

function createCompTempLineChart(sheet) {
  var startColumn = 2; // Column B
  var lastColumn = sheet.getLastColumn();
  var compTempRange = sheet.getRange(3, startColumn, 1, lastColumn - startColumn + 1); // Only Row 3

  var chart = sheet.newChart()
    .setChartType(Charts.ChartType.LINE)
    .addRange(compTempRange)
    .setPosition(5, 5, 0, 0)
    .setOption('title', 'Comp Temp (Row 3)')
    .build();

  sheet.insertChart(chart);
}

对于第 6 行图表:

function createAirMeasLineChart(sheet) {
  var startColumn = 2; // Column B
  var lastColumn = sheet.getLastColumn();
  var airMeasRange = sheet.getRange(6, startColumn, 1, lastColumn - startColumn + 1); // Only Row 6

  var chart = sheet.newChart()
    .setChartType(Charts.ChartType.LINE)
    .addRange(airMeasRange)
    .setPosition(20, 5, 0, 0)
    .setOption('title', 'Air Meas (Row 6)')
    .build();

  sheet.insertChart(chart);
}

除非需要,否则不要使用

setTransposeRowsAndColumns(true)
。这样,向其他行添加数据就不会弄乱您的图表。

© www.soinside.com 2019 - 2024. All rights reserved.