【问题标题】:Get and use user data from another sheet in Google Docs从 Google Docs 的另一个工作表中获取和使用用户数据
【发布时间】:2012-10-31 23:48:35
【问题描述】:

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

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 放到一个外部表中,并根据该表获取这些数据,这样更容易维护电子邮件和用户列表。 如何从特定的 google 电子表格中获取数据并让脚本根据它工作?

【问题讨论】:

    标签: user-controls google-apps-script google-sheets


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-02
      • 2020-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多