通过列名称在单元格中插入值

时间:2019-05-25 05:48:52

标签: javascript google-apps-script spreadsheet

我有一个Google电子表格与他人共享。我的工作表名称为“ OrderList”。我写了以下代码,我想在此代码中使用列名而不是列号。该表中冻结了第一行。

var COLUMNTOCHECK1 = 5;
var COLUMNTOCHECK2 = 6;

var DATETIMELOCATION1 = [0, 2];
var DATETIMELOCATION2 = [0, 3];

var SHEETNAME = 'OrderList'

function onEdit(e) {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getActiveSheet();
    if (sheet.getSheetName() == SHEETNAME) {
        var selectedCell = ss.getActiveCell();
        if (selectedCell.getColumn() == COLUMNTOCHECK1) {
            var email = Session.getActiveUser().getEmail();
            var dateTimeCell = selectedCell.offset(DATETIMELOCATION1[0], 
DATETIMELOCATION1[1]);
            dateTimeCell.setValue(new Date());
            var dateTimeCell1 = selectedCell.offset(DATETIMELOCATION2[0], 
DATETIMELOCATION2[1]);
            dateTimeCell1.setValue(email);
        }
        if (selectedCell.getColumn() == COLUMNTOCHECK2) {
            var dateTimeCell = selectedCell.offset(DATETIMELOCATION2[0], 
DATETIMELOCATION2[1]);
            dateTimeCell.setValue(new Date());
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您可以使用sheet.getRange(rangeOfHeaderCell).getValue()==='desired column heading')获取该列的标题单元格的值,并将其与所需内容进行比较。像这样:

if (sheet.getRange(1, selectedCell.getColumn(), 1, 1).getValue() == 'Hello World') {
    var dateTimeCell = selectedCell.offset(DATETIMELOCATION2[0], DATETIMELOCATION2[1]);
    dateTimeCell.setValue(new Date());
}

您还可以轻松地将其变成函数:

function getColumnHeading(sheet,columnIndex){
  return sheet.getRange(1,columnIndex,1,1).getValue();
}

要通过标题名称而不是偏移量获取列,要稍微复杂一点,但是我想出了一个简单的函数,该函数遍历可用的列。这是所有东西放在一起并重构的一点:

var SHEETNAME = 'OrderList';
var EMAIL_COLUMN_NAME = 'Email Stamp';
var TIMESTAMP_COLUMN_NAME = 'Time Stamp';

function onEdit(e) {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getActiveSheet();
    if (sheet.getSheetName() == SHEETNAME) {
        // Lookup columns
        var emailColumn = getColumnByHeader(EMAIL_COLUMN_NAME, sheet);
        var timestampColumn = getColumnByHeader(TIMESTAMP_COLUMN_NAME, sheet);
        // Get active cell details
        var selectedCell = ss.getActiveCell();
        var selectedColumnName = getColumnHeading(sheet,selectedCell.getColumn());
        // Set values in corresponding columns
        if (selectedColumnName == 'My Special Column') {
            var email = Session.getActiveUser().getEmail();
            if (timestampColumn) {
                sheet.getRange(selectedCell.getRow(), timestampColumn, 1, 1).setValue(new Date());
            }
            if (emailColumn){
                sheet.getRange(selectedCell.getRow(),emailColumn,1,1).setValue(email);
            }
        }
        else if (selectedColumnName == 'Hello World') {
            if (timestampColumn) {
                sheet.getRange(selectedCell.getRow(), timestampColumn, 1, 1).setValue(new Date());
            }
        }
    }
}

function getColumnByHeader(headerName, sheet, rowIndex) {
    rowIndex = typeof (rowIndex) !== 'undefined' ? rowIndex : 1;
    // Get full range of header row
    var headerRange = sheet.getRange(rowIndex, 1, 1, sheet.getDataRange().getLastColumn());
    var headerValues = headerRange.getValues()[0];
    for (var x = 0; x < headerValues.length; x++) {
        if (headerValues[x] == headerName) {
            return x + 1;
        }
    }
    return false;
}

function getColumnHeading(sheet, columnIndex) {
    return sheet.getRange(1, columnIndex, 1, 1).getValue();
}