根据Google Apps脚本中的搜索条件定义数据行

时间:2015-04-13 15:42:51

标签: javascript google-apps-script google-sheets

我正在尝试根据搜索条件将行设置为范围。我知道我想要搜索哪一列,并且我想要将该特定行作为数据范围。设置数据范围后,我将其格式化为表格并发送包含表格的电子邮件。由于我所做的所有不同尝试,我所拥有的代码非常混乱,所以我没有任何要发布的内容。下面只是行和列的示例N是我正在搜索的内容。然后我想要那个以D - Y作为我的范围的行。因此,搜索第89行的MLS / MRL周末,并将范围D89:Y89设置为基于搜索的范围。这不是很漂亮..我正在学习,这对我来说是一个非常新的领域。

    function rowOf(team1, dataRange) {
 var ss = SpreadsheetApp.openById("spreadsheetid");
 var sheet1 = ss.getSheetByName("Stats1");
 var dataRange = sheet1.getSheetValues(1,14,150,1);
 var ss1 = SpreadsheetApp.openById("otherspreadsheetid");
 var sheet = ss1.getSheetByName("Form Responses 1");
 var data = sheet.getRange(sheet.getLastRow(), 1, 1, sheet.getLastColumn()).getValues();
 var email = data[0][2];
 var team1 = data[0][1];
 var subject = team1 +" Team Stats";
 var outRow = null;

 for (var i = 0; i < dataRange.length; i++)
 {
   if (dataRange[i][0] == team1)
   {
     outRow = i+1;
     break;
   }
 }
 var Header = sheet1.getRange('d1:y1').getValues();
 var u13gneongoalsg1 = sheet1.getRange(outRow,4).getValues();
 var u13gneonshotsg1 = sheet1.getRange(outRow,5).getValues();
 var u13gneontopg1 = sheet1.getRange(outRow,6).getValues();
 var u13gneontop1g1 = sheet1.getRange(outRow,7).getValues();
 var u13gneonfirstg1 = sheet1.getRange(outRow,8).getValues();
 var u13gneonpassesg1 = sheet1.getRange(outRow,9).getValues();
 var u13gneonpasscompg1 = sheet1.getRange(outRow,10).getValues();
 var u13gneonnumpassstringg1 = sheet1.getRange(outRow,11).getValues();
 var u13gneonavgpassstringg1 = sheet1.getRange(outRow,12).getValues();
 var u13gneonlongpassstringg1 = sheet1.getRange(outRow,13).getValues();
 var u13gneonogoalsg1 = sheet1.getRange(outRow,16).getValues();
 var u13gneonoshotsg1 = sheet1.getRange(outRow,17).getValues();
 var u13gneonotopg1 = sheet1.getRange(outRow,18).getValues();
 var u13gneonotop1g1 = sheet1.getRange(outRow,19).getValues();
 var u13gneonofirstg1 = sheet1.getRange(outRow,20).getValues();
 var u13gneonopassesg1 = sheet1.getRange(outRow,21).getValues();
 var u13gneonopasscompg1 = sheet1.getRange(outRow,22).getValues();
 var u13gneononumpassstringg1 = sheet1.getRange(outRow,23).getValues();
 var u13gneonoavgpassstringg1 = sheet1.getRange(outRow,24).getValues();
 var u13gneonolongpassstringg1 = sheet1.getRange(outRow,25).getValues();


 //if (game1choice(team)) {
 var message = '<HTML><BODY><p>';
 message +=   '<table border="1">';
 message +=   '<tr><td align="center"><b>U13G Neon</b></td><td align="center"><b>Game Totals</b></td><td align="center"><b>'+team1+ '</b></td></tr>';
 message +=   '<tr><td align="center">' +u13gneongoalsg1+ '</td><td align="center"><b>Goals</b></td><td align="center">' +u13gneonogoalsg1+ '</td></tr>';
 message +=   '<tr><td align="center">' +u13gneonshotsg1+ '</td><td align="center"><b>Shots</b></td><td align="center">' +u13gneonoshotsg1+ '</td></tr>';
 message +=   '<tr><td align="center">'+u13gneontopg1 +'</td><td align="center"><b>Time of Possession</b></td><td align="center">'+u13gneonotopg1 +'</td></tr>';
 message +=   '<tr><td align="center">'+u13gneontop1g1 +'</td><td align="center"><b>Time of Possession %</b></td><td align="center">'+u13gneonotop1g1 +'</td></tr>';
 message +=   '<tr><td align="center">'+u13gneonfirstg1 +'</td><td align="center"><b>First Touches</b></td><td align="center">'+u13gneonofirstg1 +'</td></tr>';
 message +=   '<tr><td align="center">'+u13gneonpassesg1 +'</td><td align="center"><b>Passes</b></td><td align="center">'+u13gneonopassesg1 +'</td></tr>';
 message +=   '<tr><td align="center">'+u13gneonpasscompg1 +'</td><td align="center"><b>Pass Completion %</b></td><td align="center">'+u13gneonopasscompg1 +'</td></tr>';
 message +=   '<tr><td align="center">'+u13gneonnumpassstringg1 +'</td><td align="center"><b>Number of Pass Strings</b></td><td align="center">'+u13gneononumpassstringg1 +'</td></tr>';
 message +=   '<tr><td align="center">'+u13gneonavgpassstringg1 +'</td><td align="center"><b>Average Pass String Length</b></td><td align="center">'+u13gneonoavgpassstringg1 +'</td></tr>';
 message +=   '<tr><td align="center">'+u13gneonlongpassstringg1 +'</td><td align="center"><b>Longest Pass String</b></td><td align="center">'+u13gneonolongpassstringg1 +'</td></tr>';
 message +=   '</table>';
 message +=   '</p><p>';
 message += '</body></HTML>';

 MailApp.sendEmail(email, subject, "", {htmlBody: message});
 return outRow;
 } 

1 个答案:

答案 0 :(得分:0)

以下是我为我工作的代码。

    function rowOf(team1, dataRange) {
 var ss = SpreadsheetApp.openById("sheetid");
 var sheet1 = ss.getSheetByName("Stats1");
 var dataRange = sheet1.getSheetValues(1,14,300,1);
 var dataRange2 = sheet1.getSheetValues(1,2,300,1);
 var ss1 = SpreadsheetApp.openById("othersheetid");
 var sheet = ss1.getSheetByName("Form Responses 1");
 var data = sheet.getRange(sheet.getLastRow(), 1, 1, sheet.getLastColumn()).getValues();
 var email = data[0][2];
 var team1 = data[0][1];
 var magellan = data[0][3];
 var subject = team1 +" Team Stats";
 var outRow = null;
 var outRow2 = null;

  for (var i = 0; i < dataRange.length; i++){

  if (dataRange[i][0] == team1){

     outRow = i+1;

    }
  }


 var Header = sheet1.getRange('d1:y1').getValues();
 var u13gneongoalsg1 = sheet1.getRange(outRow,4).getValues();
 var u13gneonshotsg1 = sheet1.getRange(outRow,5).getValues();
 var u13gneontopg1 = sheet1.getRange(outRow,6).getValues();
 var u13gneontop1g1 = sheet1.getRange(outRow,7).getValues();
 var u13gneonfirstg1 = sheet1.getRange(outRow,8).getValues();
 var u13gneonpassesg1 = sheet1.getRange(outRow,9).getValues();
 var u13gneonpasscompg1 = sheet1.getRange(outRow,10).getValues();
 var u13gneonnumpassstringg1 = sheet1.getRange(outRow,11).getValues();
 var u13gneonavgpassstringg1 = sheet1.getRange(outRow,12).getValues();
 var u13gneonlongpassstringg1 = sheet1.getRange(outRow,13).getValues();
 var u13gneonogoalsg1 = sheet1.getRange(outRow,16).getValues();
 var u13gneonoshotsg1 = sheet1.getRange(outRow,17).getValues();
 var u13gneonotopg1 = sheet1.getRange(outRow,18).getValues();
 var u13gneonotop1g1 = sheet1.getRange(outRow,19).getValues();
 var u13gneonofirstg1 = sheet1.getRange(outRow,20).getValues();
 var u13gneonopassesg1 = sheet1.getRange(outRow,21).getValues();
 var u13gneonopasscompg1 = sheet1.getRange(outRow,22).getValues();
 var u13gneononumpassstringg1 = sheet1.getRange(outRow,23).getValues();
 var u13gneonoavgpassstringg1 = sheet1.getRange(outRow,24).getValues();
 var u13gneonolongpassstringg1 = sheet1.getRange(outRow,25).getValues();


  var message = '<HTML><BODY><p>';
 message +=   '<table border="1">';
 message +=   '<tr><td align="center"><b>U13G Neon</b></td><td align="center"><b>Game Totals</b></td><td align="center"><b>'+team1+ '</b></td></tr>';
 message +=   '<tr><td align="center">' +u13gneongoalsg1+ '</td><td align="center"><b>Goals</b></td><td align="center">' +u13gneonogoalsg1+ '</td></tr>';
 message +=   '<tr><td align="center">' +u13gneonshotsg1+ '</td><td align="center"><b>Shots</b></td><td align="center">' +u13gneonoshotsg1+ '</td></tr>';
 message +=   '<tr><td align="center">'+u13gneontopg1 +'</td><td align="center"><b>Time of Possession</b></td><td align="center">'+u13gneonotopg1 +'</td></tr>';
 message +=   '<tr><td align="center">'+u13gneontop1g1 +'</td><td align="center"><b>Time of Possession %</b></td><td align="center">'+u13gneonotop1g1 +'</td></tr>';
 message +=   '<tr><td align="center">'+u13gneonfirstg1 +'</td><td align="center"><b>First Touches</b></td><td align="center">'+u13gneonofirstg1 +'</td></tr>';
 message +=   '<tr><td align="center">'+u13gneonpassesg1 +'</td><td align="center"><b>Passes</b></td><td align="center">'+u13gneonopassesg1 +'</td></tr>';
 message +=   '<tr><td align="center">'+u13gneonpasscompg1 +'</td><td align="center"><b>Pass Completion %</b></td><td align="center">'+u13gneonopasscompg1 +'</td></tr>';
 message +=   '<tr><td align="center">'+u13gneonnumpassstringg1 +'</td><td align="center"><b>Number of Pass Strings</b></td><td align="center">'+u13gneononumpassstringg1 +'</td></tr>';
 message +=   '<tr><td align="center">'+u13gneonavgpassstringg1 +'</td><td align="center"><b>Average Pass String Length</b></td><td align="center">'+u13gneonoavgpassstringg1 +'</td></tr>';
 message +=   '<tr><td align="center">'+u13gneonlongpassstringg1 +'</td><td align="center"><b>Longest Pass String</b></td><td align="center">'+u13gneonolongpassstringg1 +'</td></tr>';
 message +=   '</table>';
 message +=   '</p><p>';
 message += '</body></HTML>';

 MailApp.sendEmail(email, subject, "", {htmlBody: message});
 return outRow;
 }