如何根据日期从列中检索数据

时间:2019-07-04 22:38:49

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

我有一个轮换表(轮换的固定顺序(根据人员或职务)),本周我已经获得了帮助。它可以按原样启动和运行,但是为了简化阅读,我想对其进行转置。

您可以按自己的意愿看到转置的图纸here

当前脚本用于预先转置的表。

它将在列0中搜索日期。如果已经7天了,它将从第1列中检索名称,并将其与单独工作表中的电子邮件地址等匹配。

我想做的是改为在第0行中包含日期,然后在第1行中包含后续名称,依此类推

我尝试了各种事情。我已经遍历了代码并且可以看到它的作用,并且已经阅读了大约2维数组,但是我似乎找不到找到使代码遍历列而不是遍历列的方法。行。

代码如下:

function sendEmails() {
  var ss1 = SpreadsheetApp.getActiveSpreadsheet();
  var sh1 = ss1.getSheetByName("Rota")
  ss1.setActiveSheet(sh1);
  var rotalink = "https://docs.google.com/spreadsheets/d/1LgzUWSAGA2kbpar8r5nosU1bSHF7nrtvtUiHS3nB_e8";
 var sheet = SpreadsheetApp.getActiveSheet();
  // Fetch the range
  var dataRange = sheet.getRange("B3:G50")
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  for (i in data)  {
    var row = data[i];
    var today=new Date();
    var timecell = new Date(row[0]);
    var timediff = new Date();
    var one_day=1000*60*60*24;
    var daystogo = Math.ceil((timecell.getTime()-today.getTime())/(one_day));
    if (daystogo==7) {//only e-mail people with one week to go. To change that alter the "7" to the number of days you want
        var subject = "Rota reminder!";
        var emailAddress = [];
        var message;
        message = "Hello \n\n"+
                 "You are down to help at Youth Café this week. \n\n" +

                  "Please see the below rota for your role \n\n" +

                  "If you have any questions or problems let us know at thameyouthcafe@gmail.com \n\n" +

                   "Remember, you can check the rota anytime by clicking on the link below: \n\n"+ 

                  rotalink



for (var x = 1; x < 5; x++) { // 5 because emails are till col4
  //  var emailAddress = []; // Start by collecting the non-blank emails in an array
    if (getEmailFromName(row[x]) != "") {
      emailAddress.push(getEmailFromName(row[x]))
    }
  }
  emailAddress = emailAddress.join();  // Join the array to get a comma separated string
  MailApp.sendEmail(emailAddress, subject, message);
    }
  }
}

这是与SKey匹配的getEmailFromName函数(我想它来自第一个函数中的“ i”变量?

function getEmailFromName(sKey) {



  // to use this function, don’t put anything in the first column (A) or row (1).

  // Put the name (i.e. the key, or what we’re looking for) in column B.

  // Put what we want to return in column C. 



  var columnToSearch = 1; //column B



  // Set the active sheet to our email lookup

  var ss1 = SpreadsheetApp.getActiveSpreadsheet();

  var sh1 = ss1.getSheetByName("EmailContactList")

  ss1.setActiveSheet(sh1);



  var data = SpreadsheetApp.getActiveSheet().getDataRange().getValues();

  var line = -1;

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

    if( data[i][columnToSearch] == sKey ) {

      line = i;

      break;

    }

  }



  if( line != -1 ) {

    //do what you want with the data on "line"

    return data[line][2]; //value on column C of the matched line

  } 

  else {

    return "";

  // if criteria is not found

 }

}

2 个答案:

答案 0 :(得分:0)

尝试一下:

volumes: - /path/from/host:/path/to/container

答案 1 :(得分:0)

设法解决-谢谢您的贡献。原来这非常简单。

只需更改此行:

var timecell = new Date(data[0])

对此:

var timecell = new Date(data[0][i])

因此它遍历每一列的第一行。