Google apps script that dynamically change color of charts based on cell hex value produce bizzarre color choice

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

Google apps script that dynamically change color of charts based on cell hex value produce bizzarre color choice

问题

我正在创建一个GSheet,其中包含一些图表,我想动态选择这些图表的颜色(我有一个包含十六进制代码的列),所以我编写了以下脚本。

由于某种奇怪的原因,至少对于我这个初学者来说,脚本以一种奇怪的方式更改颜色。大多数情况下,图表的列或切片完全是黑色或透明的。

多次执行脚本可能会产生不同的结果

第一个图表正常,第二个完全透明,第三个有2种颜色正常+透明!

第一个图表仍然正常,第二个仍然完全透明,第三个有2个黑色+透明的切片!

与第一幅图像相同,但我为第三个图表更改了十六进制代码,并点击图表,您可以看到透明列的一个值!

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('脚本')
    .addItem('更新颜色', 'changePieChartColors')
    .addToUi();
}

function changePieChartColors() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  
  // 定义每个图表的数据范围和颜色
  var chart1DataRange = sheet.getRange("D4:D9");
  var chart1ColorRange = sheet.getRange("E4:E9");
  
  var chart2DataRange = sheet.getRange("G4:G9");
  var chart2ColorRange = sheet.getRange("H4:H9");
  
  var chart3DataRange = sheet.getRange("J4:J9");
  var chart3ColorRange = sheet.getRange("K4:K9");
  
  // 配置第一个图表
  var chart1 = sheet.getCharts()[0]; // 根据图表的位置修改方括号中的数字[0]
  var data1 = chart1DataRange.getValues();
  var colors1 = chart1ColorRange.getValues();
  var colorHex1 = [];
  
  for (var i = 0; i < data1.length; i++) {
    var value = data1[i][0];
    var color = colors1[i][0];
    colorHex1.push("#" + color);
  }
  
  chart1 = chart1.modify().setOption('colors', colorHex1).build();
  sheet.updateChart(chart1);
  
  // 配置第二个图表
  var chart2 = sheet.getCharts()[1]; // 根据图表的位置修改方括号中的数字[1] - 为什么不起作用?
  var data2 = chart2DataRange.getValues();
  var colors2 = chart2ColorRange.getValues();
  var colorHex2 = [];
  
  for (var j = 0; j < data2.length; j++) {
    var value = data2[j][0];
    var color = colors2[j][0];
    colorHex2.push("#" + color);
  }
  
  chart2 = chart2.modify().setOption('colors', colorHex2).build();
  sheet.updateChart(chart2);
  
  // 配置第三个图表
  var chart3 = sheet.getCharts()[2]; // 根据图表的位置修改方括号中的数字[2] - 只显示黑色的切片!
  var data3 = chart3DataRange.getValues();
  var colors3 = chart3ColorRange.getValues();
  var colorHex3 = [];
  
  for (var k = 0; k < data3.length; k++) {
    var value = data3[k][0];
    var color = colors3[k][0];
    colorHex3.push("#" + color);
  }
  
  chart3 = chart3.modify().setOption('colors', colorHex3).build();
  sheet.updateChart(chart3);
}

我尝试更改颜色范围(以红色、蓝色等命名而不是十六进制),我还认为在脚本中添加#而不是在单元格值中添加#可能有问题,我还在图表更改之间添加了一些延迟,但没有什么改变。

期望显然是正确的颜色将被显示出来。

如果有人能指点我正确的方向,提前感谢。

PS:只有一个图表时,不会发生这种情况。

英文:

I'm creating a GSheet with some graphs, I want to choose the color of these graphs dynamically (I have a column with the hex code) so I wrote the following script.

For some strange reason, at least for my newbie level, the script change the colors in a bizzarre way. Most of the times, the column or the slices in the graph, is completely black or transparent.

Executing the script multiple time can give you different results

1st graph ok, 2nd completely transparent, 3rd 2 colors ok+transparentGoogle apps script that dynamically change color of charts based on cell hex value produce bizzarre color choice

1st graph still ok, 2nd still completely transparent, 3rd 2 slices black+transparentGoogle apps script that dynamically change color of charts based on cell hex value produce bizzarre color choice

same as first image, but I changed an hex code for the 3rd graph and clicking on the graph, you can see one value of the transparent columnGoogle apps script that dynamically change color of charts based on cell hex value produce bizzarre color choice

function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu(&#39;Script&#39;)
.addItem(&#39;Update colors&#39;, &#39;changePieChartColors&#39;)
.addToUi();
}
function changePieChartColors() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
// Definisci gli intervalli di dati e colori per ciascun grafico
var chart1DataRange = sheet.getRange(&quot;D4:D9&quot;);
var chart1ColorRange = sheet.getRange(&quot;E4:E9&quot;);
var chart2DataRange = sheet.getRange(&quot;G4:G9&quot;);
var chart2ColorRange = sheet.getRange(&quot;H4:H9&quot;);
var chart3DataRange = sheet.getRange(&quot;J4:J9&quot;);
var chart3ColorRange = sheet.getRange(&quot;K4:K9&quot;);
// Configura il primo grafico
var chart1 = sheet.getCharts()[0]; // Modifica il numero tra parentesi quadre [0] in base alla posizione del grafico
var data1 = chart1DataRange.getValues();
var colors1 = chart1ColorRange.getValues();
var colorHex1 = [];
for (var i = 0; i &lt; data1.length; i++) {
var value = data1[i][0];
var color = colors1[i][0];
colorHex1.push(&quot;#&quot; + color);
}
chart1 = chart1.modify().setOption(&#39;colors&#39;, colorHex1).build();
sheet.updateChart(chart1);
// Configura il secondo grafico
var chart2 = sheet.getCharts()[1]; // Modifica il numero tra parentesi quadre [1] in base alla posizione del grafico perch&#233; non funziona?
var data2 = chart2DataRange.getValues();
var colors2 = chart2ColorRange.getValues();
var colorHex2 = [];
for (var j = 0; j &lt; data2.length; j++) {
var value = data2[j][0];
var color = colors2[j][0];
colorHex2.push(&quot;#&quot; + color);
}
chart2 = chart2.modify().setOption(&#39;colors&#39;, colorHex2).build();
sheet.updateChart(chart2);
// Configura il terzo grafico
var chart3 = sheet.getCharts()[2]; // Modifica il numero tra parentesi quadre [2] in base alla posizione del grafico - mi da solo fette nere!
var data3 = chart3DataRange.getValues();
var colors3 = chart3ColorRange.getValues();
var colorHex3 = [];
for (var k = 0; k &lt; data3.length; k++) {
var value = data3[k][0];
var color = colors3[k][0];
colorHex3.push(&quot;#&quot; + color);
}
chart3 = chart3.modify().setOption(&#39;colors&#39;, colorHex3).build();
sheet.updateChart(chart3);
}

:

I tried to change the range of the colors (naming with red, blue... instead of hex), I also thought was something wrong in adding the # in the script instead that in the cell value, I added some sleep between the graph changes but nothing changed.

Utilities.sleep(5000); 

Expectations are, obviously, that the right color will be showed.
Thanks in advance if someone could point me in the right direction.

PS: with only one graph, it doesn't happen

答案1

得分: 0

当我看到你的脚本时,使用 setColors 而不是 setOption('colors', colorHex1) 怎么样?我猜想这可能是你当前问题的原因。当这个反映在你的脚本中时,以下修改如何?

修改后的脚本:

function changePieChartColors() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var [chart1, chart2, chart3] = sheet.getCharts();

  // 定义每个图表的数据范围和颜色
  var chart1DataRange = sheet.getRange("D4:D9");
  var chart1ColorRange = sheet.getRange("E4:E9");

  var chart2DataRange = sheet.getRange("G4:G9");
  var chart2ColorRange = sheet.getRange("H4:H9");

  var chart3DataRange = sheet.getRange("J4:J9");
  var chart3ColorRange = sheet.getRange("K4:K9");

  // 配置第一个图表
  var data1 = chart1DataRange.getValues();
  var colors1 = chart1ColorRange.getValues();
  var colorHex1 = [];

  for (var i = 0; i < data1.length; i++) {
    var color = colors1[i][0];
    colorHex1.push("#" + color);
  }

  var c1 = chart1.modify().asPieChart().setColors(colorHex1).build();
  sheet.updateChart(c1);

  // 配置第二个图表
  var data2 = chart2DataRange.getValues();
  var colors2 = chart2ColorRange.getValues();
  var colorHex2 = [];

  for (var j = 0; j < data2.length; j++) {
    var color = colors2[j][0];
    colorHex2.push("#" + color);
  }

  var c2 = chart2.modify().asColumnChart().setColors(colorHex2).build();
  sheet.updateChart(c2);

  // 配置第三个图表
  var data3 = chart3DataRange.getValues();
  var colors3 = chart3ColorRange.getValues();
  var colorHex3 = [];

  for (var k = 0; k < data3.length; k++) {
    var color = colors3[k][0];
    colorHex3.push("#" + color);
  }

  var c3 = chart3.modify().asPieChart().setColors(colorHex3).build();
  sheet.updateChart(c3);
}

测试:

当使用你提供的电子表格运行此脚本时,将获得以下结果。

Google apps script that dynamically change color of charts based on cell hex value produce bizzarre color choice

注意:

  • 此修改后的脚本适用于你提供的电子表格。当你更改电子表格时,此脚本可能无法使用。请注意这一点。

参考链接:

英文:

When I saw your script, how about using setColors instead of setOption(&#39;colors&#39;, colorHex1)? I guessed that this might be the reason for your current issue. When this is reflected in your script, how about the following modification?

Modified script:

function changePieChartColors() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var [chart1, chart2, chart3] = sheet.getCharts();

  // Definisci gli intervalli di dati e colori per ciascun grafico
  var chart1DataRange = sheet.getRange(&quot;D4:D9&quot;);
  var chart1ColorRange = sheet.getRange(&quot;E4:E9&quot;);

  var chart2DataRange = sheet.getRange(&quot;G4:G9&quot;);
  var chart2ColorRange = sheet.getRange(&quot;H4:H9&quot;);

  var chart3DataRange = sheet.getRange(&quot;J4:J9&quot;);
  var chart3ColorRange = sheet.getRange(&quot;K4:K9&quot;);

  // Configura il primo grafico
  var data1 = chart1DataRange.getValues();
  var colors1 = chart1ColorRange.getValues();
  var colorHex1 = [];

  for (var i = 0; i &lt; data1.length; i++) {
    var color = colors1[i][0];
    colorHex1.push(&quot;#&quot; + color);
  }

  var c1 = chart1.modify().asPieChart().setColors(colorHex1).build();
  sheet.updateChart(c1);

  // Configura il secondo grafico
  var data2 = chart2DataRange.getValues();
  var colors2 = chart2ColorRange.getValues();
  var colorHex2 = [];

  for (var j = 0; j &lt; data2.length; j++) {
    var color = colors2[j][0];
    colorHex2.push(&quot;#&quot; + color);
  }

  var c2 = chart2.modify().asColumnChart().setColors(colorHex2).build();
  sheet.updateChart(c2);

  // Configura il terzo grafico
  var data3 = chart3DataRange.getValues();
  var colors3 = chart3ColorRange.getValues();
  var colorHex3 = [];

  for (var k = 0; k &lt; data3.length; k++) {
    var color = colors3[k][0];
    colorHex3.push(&quot;#&quot; + color);
  }

  var c3 = chart3.modify().asPieChart().setColors(colorHex3).build();
  sheet.updateChart(c3);
}

Testing:

When this script is run using your provided Spreadsheet, the following result is obtained.

Google apps script that dynamically change color of charts based on cell hex value produce bizzarre color choice

Note:

  • This modified script is for your provided Spreadsheet. When you change the Spreadsheet, this script might not be able to be used. Please be careful about this.

References:

huangapple
  • 本文由 发表于 2023年6月22日 16:02:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76529735.html
匿名

发表评论

匿名网友

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

确定