查找给定标题行名称的列 [英] Find column given name of title row

查看:59
本文介绍了查找给定标题行名称的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一些表格:

通过在:

I'm able to get the B-column's first row value datetime by hardcoding column index in A1 notation, "B:B", into my onEdit function:

function onEdit(event)
{
  var datetime_col = "B:B";
  Logger.log(event.range.getSheet().getRange(datetime_col).getValue());
  //=> [17-03-06 11:39:47:752 PST] datetime
}

但是我想要给定列ID "datetime"的列ID.

But I want the column id given the string "datetime".

对我而言,列是否表示为范围,字符串"B:B",字符串"B"或其他标识符对我来说都没有关系.重要的一点是我想查找具有该名称的列,而不是相反.

It doesn't matter to me if the column is represented as a range, the string "B:B", the string "B", or some other identifier. The important bit is I want to find the column given the name not the other way around.

推荐答案

将数据的第一行移至最后一列,然后使用indexOf(col_Title)

Get the first row of data to the last column, then use indexOf(col_Title)

function onEdit(event) {
  var columnNeeded,data,lastColumn,sh;

  sh = SpreadsheetApp.getActiveSheet();
  lastColumn = sh.getLastColumn();

  data = sh.getRange(1,1,1,lastColumn).getValues();//Get 2D array of all values in row one
  data = data[0];//Get the first and only inner array

  columnNeeded = data.indexOf('datetime') + 1;//Arrays are zero indexed- add 1

  Logger.log('columnNeeded: ' + columnNeeded);

}

这篇关于查找给定标题行名称的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆