根据今天的日期在 Google 表格中移动一行

问题描述 投票:0回答:1

我正在尝试制作一个电子表格来帮助我的祖父母跟踪他们的医生预约。他们希望有一张表可以写下所有即将到来的约会(每行一个约会),然后在约会结束后将该行移至另一张表。

我能够找到之前的帖子,其中有人有相同的目标。 用户 Pierre-Marie Richard 发布了以下代码作为解决方案:

function approveRequests() {

  // Initialising
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var scheduleSheet = ss.getSheetByName("Flight Schedule");
  var pastSheet = ss.getSheetByName("Past Flight");
  var lastColumn = scheduleSheet.getLastColumn();

  // Check all values from your "Flight Schedule" sheet
  for(var i = scheduleSheet.getLastRow(); i > 0; i--){

    // Check if the value is a valid date
    var dateCell = scheduleSheet.getRange(i, 1).getValue();
    if(isValidDate(dateCell)){
      var today = new Date();
      var test = new Date(dateCell);

      // If the value is a valid date and is a past date, we remove it from the sheet to paste on the other sheet
      if(test < today){

        var rangeToMove = scheduleSheet.getRange(i, 1, 1, scheduleSheet.getLastColumn()).getValues();
        pastSheet.getRange(pastSheet.getLastRow() + 1, 1, 1, scheduleSheet.getLastColumn()).setValues(rangeToMove);
        scheduleSheet.deleteRow(i);

      }
    }
  }
}

// Check is a valid date
function isValidDate(value) {
  var dateWrapper = new Date(value);
  return !isNaN(dateWrapper.getDate());

此解决方案确实将行从 Sheet1(“航班时刻表”)移动到 Sheet2(“过去的航班”)。但是,它似乎是根据特定行中是否存在具有时间值的单元格来移动行。它将如果有时间值则移动该行,如果没有时间值则不会移动该行(无论输入任何日期)。

我真的不知道为什么会这样做。看起来问题出在

var today = new Date();
,但这对我来说没有意义?

我确信解决方案非常简单,但我是编码的超级初学者。我真的很想帮助我的祖父母解决这个问题。任何帮助将不胜感激!

javascript google-sheets google-apps-script
1个回答
0
投票

每次打开电子表格时运行存档可能是最简单的方法。如果它一直保持打开状态,您可以添加一个菜单项来让用户轻松运行代码,如下所示:

/**
* Simple trigger that runs each time the user opens the spreadsheet.
*
* @param {Object} e The onOpen() event object.
*/
function onOpen(e) {
  moveRowsWhereDateIsInThePast();
  SpreadsheetApp.getUi()
    .createMenu('Archive')
    .addItem('Move past appointments to archive', 'moveRowsWhereDateIsInThePast')
    .addToUi();
}


/**
* Moves rows where a date in a column is in the past to another sheet.
*/
function moveRowsWhereDateIsInThePast() {
  // version 1.1, written by --Hyde, 23 May 2024
  //  - see https://stackoverflow.com/q/78521240/13045193
  const ss = SpreadsheetApp.getActive();
  const dateRange = ss.getRange('Appointments!D1:D');
  const targetSheet = ss.getSheetByName('Past appointments');
  const sourceSheet = dateRange.getSheet();
  const timezone = ss.getSpreadsheetTimeZone();
  const dateString_ = (date) => Utilities.formatDate(date, timezone, 'yyyy-MM-dd');
  const todayString = dateString_(new Date());
  const dateStrings = dataRange.getValues().flat()
    .map(date => date.getTime ? dateString_(date) : null);
  const data = sourceSheet.getDataRange().getValues();
  let numRowsMoved = 0;
  for (let rowIndex = dateStrings.length - 1; rowIndex >= 0; rowIndex--) {
    if (!dateStrings[rowIndex] || dateStrings[rowIndex] >= todayString) {
      continue;
    }
    targetSheet.appendRow(data[rowIndex]);
    sourceSheet.deleteRows(rowIndex + 1, 1);
    numRowsMoved += 1;
  }
  if (numRowsMoved) {
    const message = `Moved ${numRowsMoved} rows from '${sourceSheet.getName()}' to '${targetSheet.getName()}'.`;
    console.log(message);
    ss.toast(message);
  }
}

请参阅简单触发器Utilities.formatDate()

© www.soinside.com 2019 - 2024. All rights reserved.