尝试放弃Excel for Google表格。 我有一个空表,上面有一些彩色的单元格,需要用符号填充。目前,我使用此VBA脚本来完成这项工作:
Sub mark()
Dim r As Range
Dim rCell As Range
Set r = Selection.Cells
For Each rCell In r
If rCell.Interior.ColorIndex = 10 Then rCell.Value = "×"
If rCell.Interior.ColorIndex = 3 Then rCell.Value = "×"
If rCell.Interior.ColorIndex = 45 Then rCell.Value = "×"
If rCell.Interior.ColorIndex = 1 Then rCell.Value = "×"
If rCell.Interior.ColorIndex = 15 Then rCell.Value = "×"
Next
End Sub
是否可以使用Google表格来完成相同的事情?
答案 0 :(得分:3)
要实现此目的,您将必须使用Google Apps脚本。您可以通过导航Tools > Script Editor
将Apps脚本项目附加到Google电子表格中。
您应该找到名为myFunction
的模板函数,这是脚本的理想起点。
您可以在此处开始将VBA脚本转换为与Java脚本非常相似的Apps脚本。
首先,您应该为脚本定义一些常量:
// An array containing the color codes you want to check
const colors = ['#00ff00']; // Watch out, it's case sensitive
// A reference to the attached Spreadsheet
const ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('SheetName'); // Selecting the Worksheet we want to work with by name
// Here we retrieve the color codes of the backgrounds of the range we want to check
const range = ss.getDataRange().getBackgrounds(); // Here I select all the cells with data in them
现在让我们遍历范围行和列以应用逻辑:
.getBackgrounds()
方法以array[row][column] -> "background-color-code"
的形式返回多维数组。
for (let i = 0; i<range.length; i++) {
let row = range[i];
// Let's loop through the row now
for (let j = 0; j< row.length; j++) {
let color = row[j];
// If the background color code is among the ones we are checking we set the cell value to "x"
if(colors.includes(color)) {
// Javascript index notation is 0 based, Spreadsheet one though, starts from 1
ss.getRange(i+1, j+1).setValue("x"); // Let's add 1 to our indexes to reference the correct cell with the .getRange(row, column) function
}
}
}
请查看文档以获取进一步的阅读和方法规范