0

我们使用谷歌电子表格来报告大量用户。我编写了一个基本脚本,它根据当前用户打开一个特定的工作表:

var CurrentUser = Session.getUser().getEmail()
var ss = SpreadsheetApp.getActive();
  switch(CurrentUser){
    case "usermail1@gmail.com":
      SpreadsheetApp.setActiveSheet(ss.getSheetByName("sheet1"));
      break;
    case "usermail2@gmail.com":
      SpreadsheetApp.setActiveSheet(ss.getSheetByName("sheet2"));
      break;
    case "usermail3@gmail.com":
      SpreadsheetApp.setActiveSheet(ss.getSheetByName("sheet3"));
      break;
    etc...

我想将 userdata 和 sheetnames 放入一个外部表中,并根据该表获取这些数据,因此更容易维护电子邮件和用户列表。如何从特定的谷歌电子表格中获取数据并让脚本根据它工作?

4

1 回答 1

0

你可以试试这个。它在不同的工作表上模拟 aVLOOKUP并切换到当前工作簿中的“匹配”工作表。这不处理不匹配的情况,但这应该相对简单地添加以适合您的情况。

function switchSheets() {
  // Current sheet
  var ss = SpreadsheetApp.getActive();
  // Target sheet (using the key found in the URL)
  var target = SpreadsheetApp.openById("my_other_sheet_id");
  var rows = target.getDataRange();
  var values = rows.getValues();

  // Get the current user
  var CurrentUser = Session.getUser().getEmail();

  // Now iterate through all of the rows in your target sheet,
  // looking for a row that matches your current user (this assumes that
  // it will find a match - you'll want to handle non-matches as well)
  for (var i = 1; i <= rows.getNumRows() - 1; i++) {
    // Match the leftmost column
    var row = values[i][0];
    if (row == CurrentUser) {
      // If we match, grab the corresponding sheet (one column to the right)
      target_sheet = values[i][1];
      break;
    }
  }
  // Now switch to the matched sheet (rememeber to handle non-matches)
  ss.setActiveSheet(ss.getSheetByName(target_sheet));
};
于 2012-11-12T15:32:08.453 回答