假设我有很多列,其中一列包含“印象”字符串(第3行)。我需要做的是: 1)找到带有“印象”字符串的单元格 2)获取列号或即“D” 3)基于我将公式粘贴到D2细胞中,从D4范围获得AVERAGE:D * last *
我无法在任何地方找到它所以我不得不在这里问任何“样本”代码,因为我不知道如何实现我想要的。 (第三个很容易,但我需要先得到“D”)
答案 0 :(得分:1)
无法在Google Apps脚本中搜索。下面是一个函数,它将为您完成前两个部分(通过遍历第3行中的每个单元格并查找"印象"):
function findColumnNumber() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet1'); // insert name of sheet here
var range = sheet.getDataRange(); // get the range representing the whole sheet
var width = range.getWidth();
// search every cell in row 3 from A3 to the last column
for (var i = 1; i <= width; i++) {
var data = range.getCell(3,i)
if (data == "impressions") {
return(i); // return the column number if we find it
}
}
return(-1); // return -1 if it doesn't exist
}
希望这可以让你完成你需要做的事情!
答案 1 :(得分:0)
indexOf
方法允许搜索字符串:
function findColumnNumber() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet() //whatever tab the code is run on
var data = sheet.getDataRange().getValues();
var header_row_num = 1; // TODO: change this to whichever row has the headers.
var header = data[header_row_num -1] //Remember JavaScript, like most programming languages starts counting (is indexed) at 0. For the value of header_row_num to work with a zero-index counting language like JavaScript, you need to subtract 1
//define the string you want to search for
var searchString = "impressions";
//find that string in the header and add 1 (since indexes start at zero)
var colNum = header.indexOf(searchString) + 1;
return(colNum);