简体   繁体   English

通过带有数组的脚本在 Google Sheet 中设置单元格颜色

[英]Set Cell Colours in Google Sheet through Script with an Array

It's very straight forward to get a sheets cell values into an array.将工作表单元格值放入数组非常简单。 Then you can edit any individual cell or cells you want (In the array).然后您可以编辑您想要的任何单个单元格或单元格(在数组中)。 Then write that same array back to the sheet.然后将相同的数组写回工作表。 Like below.像下面。

var adSpendExprtSheet = ss.getSheetByName("Ad Spend Export");
var adSpendExprtSheetData = adSpendExprtSheet.getRange(1 ,1, adSpendExprtSheet.getLastRow(), adSpendExprtSheet.getLastColumn()).getValues();

Then you can change values in the array just saved.然后您可以更改刚刚保存的数组中的值。

adSpendExprtSheetData[0][0] = "Changing first cell in array"

Then we can use this same array.然后我们可以使用这个相同的数组。 Which we have updated.我们已经更新了。 Passing it back to be written on the actual sheet.将其传回以写在实际工作表上。

adSpendExprtSheet.getRange(1 ,1, adSpendExprtSheet.getLastRow(), adSpendExprtSheet.getLastColumn()).setValues(adSpendExprtSheetData);

Can we do this for setting colours?我们可以这样做来设置颜色吗?

Right now I would have to get the range of individual cells.现在我必须获得单个单元格的范围。 Then use setBackground("#00ff00");然后使用 setBackground("#00ff00");

Can I put all colour values into an array?我可以将所有颜色值放入一个数组中吗? Change the colours of cells with a HEX value.使用 HEX 值更改单元格的颜色。 Then write that array back to the sheet?然后将该数组写回工作表?

I need to optimize my script.我需要优化我的脚本。 Instead of taking 5 minutes.而不是花5分钟。 It could be done in seconds.它可以在几秒钟内完成。 By reading and writing to the sheet only a few times.只需在工作表上读写几次。 Not hundreds.不是数百。

I would appreciate any help!我将不胜感激任何帮助!

In your case, how about using getBackgrounds() and setBackgrounds(color) ?在您的情况下,如何使用getBackgrounds()setBackgrounds(color) In this case, you can use it like getValues() and setValues() in your question.在这种情况下,您可以在问题中像getValues()setValues()一样使用它。 When your script is modified, it becomes as follows.当你的脚本被修改时,它变成如下。

Modified script:修改后的脚本:

var ss = SpreadsheetApp.getActiveSpreadsheet();
var adSpendExprtSheet = ss.getSheetByName("Ad Spend Export");
var range = adSpendExprtSheet.getRange(1 ,1, adSpendExprtSheet.getLastRow(), adSpendExprtSheet.getLastColumn());
var backgrounds = range.getBackgrounds();
backgrounds[0][0] = "#FF0000";  // This is red color as a sample.
range.setBackgrounds(backgrounds);
  • In this case, the value retrieved with getBackgrounds() is 2 dimensional array.在这种情况下,使用getBackgrounds()检索的值是二维数组。 And it can be put to the cells using setBackgrounds() .并且可以使用setBackgrounds()将其放入单元格。 By this, I think that the process cost will be able to be reduced than that of getBackground and setBackground .这样,我认为流程成本将能够比getBackgroundsetBackground降低。

Note:笔记:

  • If you want to set one color to the range, you can also use the following script.如果要为范围设置一种颜色,也可以使用以下脚本。

     var ss = SpreadsheetApp.getActiveSpreadsheet(); var adSpendExprtSheet = ss.getSheetByName("Ad Spend Export"); var range = adSpendExprtSheet.getRange(1 ,1, adSpendExprtSheet.getLastRow(), adSpendExprtSheet.getLastColumn()); range.setBackground("#FF0000");

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过从 Google Sheet 生成的数组进行交互 | Google Apps 脚本 - Interact through an array generated from a Google Sheet | Google Apps Script 清除单元格谷歌工作表脚本 - Clear cell google sheet script 遍历数组并突出显示数据为[Google表格脚本]的行 - Iterating through an array and highlighting row where data is [Google Sheet script] 如何通过 Google Sheet API 为 Google Sheet Cell 指定颜色 - How to specify colors for a Google Sheet Cell through the Google Sheet API Google脚本将单元格的一部分复制到另一个工作表 - Google Script Copying part of a cell to another sheet Google表格脚本可为特定单元格添加时间 - Google Sheet Script To Add Time To Specific Cell Google Apps Script setFormula 如何将公式设置为包含变量的表格单元格 - How can Google Apps Script setFormula set a formula to sheet cell containing variables 用于检索要在 Google Sheet 数组中设置的 Google Drive 文档超链接的 Google 脚本 - Google script to retrieve Google Drive documents hyperlink to be set in a Google Sheet array 是否可以将 Google Apps 脚本数组值作为纯文本插入工作表单元格? - Is it possible to insert a Google Apps Script array value into a Sheet cell as plain text? 如何使用脚本访问 Google 工作表中选定单元格旁边的单元格? - How to access the cell next to the selected cell in Google sheet using script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM