繁体   English   中英

通过Google表格中的列名将一个单元格值从一个工作表提取到另一个工作表

[英]Extract a cell value from one sheet to another by column name in Google Sheets

我有其中包括一个模拟式电子表格在这里

我编写了一个代码,该代码采用下拉值“ Completed”,并且当选择“ Completed”时,它将发布当前日期:

function onAnotherSwitch() {
var ss = SpreadsheetApp.getActive()
var sheet      = ss.getSheetByName("Meeting Completed");
var activeCell = sheet.getActiveCell();
var col        = activeCell.getColumn();
var row        = activeCell.getRow();
if (col == 10 && activeCell.getValue() === "Completed") {
sheet.getRange(row, col+1).setValue(new    
Date()).setNumberFormat('MM/dd/yyyy');
} 
}

我正在尝试编写另一个函数,该函数在选择“已完成”时执行完全相同的操作,而是从“会议成员列表”表中获取Ytd出勤,并将Ytd出勤值放在时间戳记右侧的两个空格处工作表“会议已完成”。 YTD应与与会者的姓名相对应。 另外,由于我计划在这两个表中添加更多列,因此我试图使其按列名搜索这些值。

我最接近的就是这种程度的东西:

function onAnotherSwitch() {
var ss = SpreadsheetApp.getActive()
var sheet      = ss.getSheetByName("Meeting Completed");
var activeCell = sheet.getActiveCell();
var col        = activeCell.getColumn();
var row        = activeCell.getRow();
if (col == 10 && activeCell.getValue() === "Completed") {
sheet.getRange(row, col+1).setValue(
} 
}

您没有指定哪一列具有与会者的姓名,该姓名用于在两张工作表之间匹配数据。 传统上,此类数据在第一列中,因此我认为是。 然后脚本可以是这样的:

function attendance() {
  var ss = SpreadsheetApp.getActive()
  var sheet      = ss.getSheetByName("Meeting Completed");
  var activeCell = sheet.getActiveCell();
  var col        = activeCell.getColumn();
  var row        = activeCell.getRow();
  if (col == 10 && activeCell.getValue() === "Completed") {
    var name = sheet.getRange(row, 1).getValue();   // get the name from column A
    var listSheet = ss.getSheetByName("List of Meeting Members");
    var data = listSheet.getDataRange().getValues();  // all data from that sheet
    var colIndex = data[0].indexOf('Ytd Attendance'); // found column with attendance
    var attendance = 'N/A';   // in case there's no data for this person
    for (var i = 1; i < data.length; i++) {
      if (data[i][0] == name) {            // found the name
        attendance = data[i][colIndex];    // took attendance
        break;
      }
    }
    sheet.getRange(row, col+2).setValue(attendance);
  }
}

阅读此内容时,请记住,方括号中的索引以0开头(这在JavaScript中是标准的),而在表格中,列/行号以1开头。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM