简体   繁体   中英

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

I have a mock-up spreadsheet which is included here !

I have written a code that takes the dropdown value "Completed" and when this "Completed" selected, it posts the current date:

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');
} 
}

I am trying to write another function that does the exact same thing when "Completed" is selected but instead takes the Ytd Attendance from the sheet "List of Meeting Members" and puts the Ytd Attendance value two space to the right of the time stamp in sheet "Meeting Completed." The YTD should correspond to the name of the attendee. Additionally, since I am planning to add more columns to both of these sheets, I am trying to have it search for these values by column name.

The closest I've come is something to this extent:

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(
} 
}

You didn't specify which column has the name of the attendee, which is to be used for matching data between two sheets. Traditionally, this sort of data is in the first column so I assume that it is. Then the script can be this:

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);
  }
}

When reading this, keep in mind that the indices in square brackets begin with 0 (as is standard in JavaScript), while in Sheets, column/row numbers begin with 1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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