我的应用程序一直显示今天的日期,而不是我要求的单元格中的内容

时间:2017-09-13 21:37:25

标签: google-apps-script google-sheets

我得到列日期的第一个日期的输出,其中“exp”应该是。第3列是所有到期日期,但是当我调用并输出它时它显示为长格式化日期日期和时间。我对编码知之甚少。请帮忙。

这是输出应该是什么样子,除了日期应该是从纸张的最近日期开始的日期。

MJC-AP-200 Watchguard型号:AP 200将于09/30/17在10天内到期。

DMA-AP-300-2 Watchguard型号:AP 300将于09/30/17 0天到期。

Elwood Properties Watchguard型号:WGD-WG026583将于09/30/17在0天内到期。

Elwood Prop-XTM-26W Watchguard型号:XTM 26-Wis将于2017年7月30日0天到期。

  function checkReminder() {
// get the spreadsheet object
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// set the first sheet as active
SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);
// fetch this sheet
var sheet = spreadsheet.getActiveSheet();

// figure out what the last row is
var lastRow = sheet.getLastRow();

// the rows are indexed starting at 1, and the first row
// is the headers, so start with row 2
var startRow = 2;

// grab column 8 (the 'days left' column) 
var range = sheet.getRange(2,8,lastRow-startRow+1,1 );
var numRows = range.getNumRows();
var days_left_values = range.getValues();

// Now, grab the name column
range = sheet.getRange(2, 1, lastRow-startRow+1, 1);
var reminder_info_values = range.getValues();

// Now, grab the exp date column
range = sheet.getRange(2, 3, lastRow-startRow+1, 1);
var exp = range.getValue();
var numRows = range.getNumRows();
var exp = Utilities.formatDate(exp, "GMT+1", "MM/dd/yy")

// Now, grab the model column
range = sheet.getRange(2, 4, lastRow-startRow+1, 1);
var model = range.getValues();

var warning_count = 0;
var msg = "";

// Loop over the days left values
for (var i = 0; i <= numRows - 1; i++) {
var days_left = days_left_values[i][0];
if(days_left <= 60) {
  // if it's less than 60 days until due date.
  var reminder_name = reminder_info_values[i][0];

  msg = msg + ""+reminder_name+" Watchguard model : " +model[i][0]+"is due 
  in "+days_left+" days "+Utilities.formatDate(exp[i]
  [0],"GMT+1","MM/dd/yy")+".\n \n";
  warning_count++;
  }
  }

  if(warning_count) {
   MailApp.sendEmail("James@mccnet.com", 
    "Watchguard Expiration List", msg);
   }


   };

1 个答案:

答案 0 :(得分:1)

通过快速查看,您似乎将formatDate方法new Date()作为第一个实际返回今天日期的参数。我想你想要的方式就是这个

var exp = Utilities.formatDate(exp,"GMT+1","MM/dd/yy") 

这实际上为formatDate提供了从电子表格中提取的日期,并且格式化了正确的日期。

请尝试使用此代码:

function checkReminder() {
// get the spreadsheet object
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// set the first sheet as active
SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);
// fetch this sheet
var sheet = spreadsheet.getActiveSheet();

// figure out what the last row is
var lastRow = sheet.getLastRow();

// the rows are indexed starting at 1, and the first row
// is the headers, so start with row 2
var startRow = 2;

// grab column 8 (the 'days left' column) 
var range = sheet.getRange(2,8,lastRow-startRow+1,1 );
var numRows = range.getNumRows();
var days_left_values = range.getValues();

// Now, grab the name column
range = sheet.getRange(2, 1, lastRow-startRow+1, 1);
var reminder_info_values = range.getValues();

// Now, grab the exp date column
range = sheet.getRange(2, 3, lastRow-startRow+1, 1);
var exp = range.getValues();
var numRows = range.getNumRows();

// Now, grab the model column
range = sheet.getRange(2, 4, lastRow-startRow+1, 1);
var model = range.getValues();

var warning_count = 0;
var msg = "";

// Loop over the days left values
for (var i = 0; i <= numRows - 1; i++) {
var days_left = days_left_values[i][0];
if(days_left <= 60) {
  // if it's less than 60 days until due date.
  var reminder_name = reminder_info_values[i][0];

  msg = msg + ""+reminder_name+" Watchguard model : " +model[i][0]+"is due 
  in "+days_left+" days "+Utilities.formatDate(exp[i]
  [0],"GMT+1","MM/dd/yy")+".\n \n";
  warning_count++;
  }
  }

  if(warning_count) {
   MailApp.sendEmail("James@mccnet.com", 
    "Watchguard Expiration List", msg);
  }
};