-1

I found a script on the Internet and am trying to alter it a little to fit my needs. But stumbled upon a problem. The script takes a list of bookmarks from the drop down menu and if you write OK in the next cell, then it transfers the line to the selected bookmark. I want to confirm today's date.

Copy of the sheet. Also here some diffeent.

var actionCol = 7; var nameCol = 6;

https://docs.google.com/spreadsheets/d/1P9avWP8i5Z4KA4zt3EoUZNbgMKeXwnOYKsgCG2_vSLE/edit?usp=sharing

/**
 * Moves row of data to another spreadsheet based on criteria in column 6 to sheet with same name as the value in column 4.
*/
 
function onEdit(e) {
  // see Sheet event objects docs
  // https://developers.google.com/apps-script/guides/triggers/events#google_sheets_events
  var ss = e.source;
  var s = e.range.getSheet();
  var r = e.range;
  
  // to let you modify where the action and move columns are in the form responses sheet
  var actionCol = 10;
  var nameCol = 9;
 
  // Get the row and column of the active cell.
  var rowIndex = r.getRowIndex();
  var colIndex = r.getColumnIndex();
   
  // Get the number of columns in the active sheet.
  // -1 to drop our action/status column
  var colNumber = s.getLastColumn()-1;
   
  // if our action/status col is changed to ok do stuff
  if (e.value == "ok" && colIndex == actionCol) {
    // get our target sheet name - in this example we are using the priority column
    var targetSheet = s.getRange(rowIndex, nameCol).getValue();
    // if the sheet exists do more stuff
    if (ss.getSheetByName(targetSheet)) { 
      // set our target sheet and target range
      var targetSheet = ss.getSheetByName(targetSheet);
      var targetRange = targetSheet.getRange(targetSheet.getLastRow()+1, 1, 1, colNumber);
      // get our source range/row
      var sourceRange = s.getRange(rowIndex, 1, 1, colNumber);
      // new sheets says: 'Cannot cut from form data. Use copy instead.' 
      sourceRange.copyTo(targetRange);
      // ..but we can still delete the row after
      s.deleteRow(rowIndex);
      // or you might want to keep but note move e.g. r.setValue("moved");
    }
  }
}
UnrealHA
  • 3
  • 2
  • 1
    `But stumbled upon a problem.` Can you be more specific? Are you getting any error? How's the actual outcome differing from the one you expect? Can you provide a copy of the spreadsheet you are working on, indicating this difference? – Iamblichus Nov 16 '20 at 14:09

2 Answers2

0

Try it this way:

function onEdit(e) {
  var ss = e.source;
  var s = e.range.getSheet();
  var r = e.range;
  var actionCol = 10;
  var nameCol = 9;
  var rowIndex = r.rowStart;
  var colIndex = r.columnStart;
  var colNumber = s.getLastColumn()-1;
  const dt=new Date(e.range.offset(0,n));//you add the column offset 
  const dtv=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();
  const td=new Date();
  const tdv=new Date(td.getFullYear(),td.getMonth(),td.getDate()).valueOf();
  if (e.value == "ok" && colIndex == actionCol && dtv==tdv) {
    var targetSheet = s.getRange(rowIndex, nameCol).getValue();
    var tSh = ss.getSheetByName(targetSheet);
    if (tSh) { 
      var targetRange = tSh.getRange(tSh.getLastRow()+1, 1, 1, colNumber);
      var sourceRange = s.getRange(rowIndex, 1, 1, colNumber);
      sourceRange.copyTo(targetRange);
      s.deleteRow(rowIndex);
    }
  }
}
Cooper
  • 59,616
  • 6
  • 23
  • 54
0

If I understand you correctly, you want to cut & paste the edited row if:

  1. The edited column is G, and the value is a Date corresponding to today.
  2. The corresponding name in column F refers to the name of an existing sheet.

In order to check that the two dates correspond to the same day, you can compare the values returned by getDate(), getMonth() and getFullYear(), as in this function (credits to Pointy):

function sameDay(d1, d2) {
  return d1.getFullYear() === d2.getFullYear() &&
         d1.getMonth() === d2.getMonth() &&
         d1.getDate() === d2.getDate();
}

Then, you can call this in your main function:

function onEdit(e) {
  var ss = e.source;
  var s = e.range.getSheet();
  var r = e.range;
  var value = r.getValue();
  var actionCol = 7;
  var nameCol = 6;
  var rowIndex = r.getRow();
  var colIndex = r.getColumn();
  var colNumber = s.getLastColumn();
  if (value instanceof Date && colIndex == actionCol) {
    var today = new Date();
    if (sameDay(value, today)) {
      var targetSheetName = s.getRange(rowIndex, nameCol).getValue();
      var targetSheet = ss.getSheetByName(targetSheetName);
      if (targetSheet) {
        var sourceRange = s.getRange(rowIndex, 1, 1, colNumber);
        var targetRange = targetSheet.getRange(targetSheet.getLastRow()+1, 1);
        sourceRange.copyTo(targetRange);
        s.deleteRow(rowIndex);
      } 
    }
  }
}
Iamblichus
  • 18,540
  • 2
  • 11
  • 27
  • now the function works in such a way that if the tab is selected in the "F" column and the "G" column was changed to "ok" then the line is transferred. I want to change so that for the place "ok" in the column "G" the date is displayed and if it is today's date, then copying occurs. Logically, I understand how everything goes, but here I have to write wild problems myself. not given: D Thank you very much in advance. – UnrealHA Nov 18 '20 at 09:13
  • @UnrealHA The script is already checking if it's today's date, even if the `ok` is not changing to a date. Do you mean you want the date in `C` to get copied to `G`, then compare the date to today's date, and then, if it matches, move the row to the destination sheet? – Iamblichus Nov 18 '20 at 09:53
  • instead of "OK" write date, and then check if the date today then copy. – UnrealHA Nov 18 '20 at 12:51
  • @UnrealHA So you want to copy the data if today's date is written to `G`, disregarding `C`. I edited my answer, including the code snippet, in order to do this. Let me know if that works for you. – Iamblichus Nov 18 '20 at 13:19
  • @UnrealHA `Not work` can you please be more precise? Are you getting any error? I have tested this before posting, it works. Have you copied the script as it is, including the function `sameDay`? – Iamblichus Nov 18 '20 at 15:00
  • @UnrealHA Make sure that (1) `sameDay` is copied to your script (you haven't copied it to the sample you provided), (2) you write a valid date, with the valid format according to your spreadsheet settings (in your case you should write `11/18/2020` I think) - if you mix up days and months, you might not get a valid date, since a year does not have an 18th month!, and (3) column `F` contains a valid sheet name **before** adding the date to `G`. – Iamblichus Nov 18 '20 at 15:13
  • 1
    I understood where my problem was. Thank you so much. Everything works great. – UnrealHA Nov 19 '20 at 07:23