0

I've been digging around on line for a couple of hours now to no avail. I'm trying to write a formula(which I assume is only possible through AppScript), that based on a certain date, will re-paste contents in a cell as values only.

The issue I've had with other scripts is that in my situation I have several different values that need to be checked. I've set up a continuously updating calendar that pulls from an array with listed dates for each item. However, in that array, the row gets moved into an archive once it hits todays date. Thus, they get deleted from the calendar too.

If there is another solution to this, like pasting based off conditional formatting(this would then keep the formulas that haven't found a value yet, and paste ones that have been found as values only), that would be great too.

Any help or advice would be greatly appreciated. If you need any other info, please ask. EDIT: The spreadsheet link is in comments. Attached are a before and after picture to help visualize whats going on here. Since the calendar is pulling from the active sheet, and rows on the active sheet go to "archive" when J2-Today() is -1(yesterday), those then disappear from the calendar. I've tried to make a mixed sheet with both archive and active, but I get duplicates as a frequent issue.

What the calendar looks like on 8/4/21

What the calendar will look like on 8/5/21

NightEye
  • 10,634
  • 2
  • 5
  • 24
Bobsleder
  • 13
  • 4
  • Please provide what have you done so far and a sample data you are working on for us to be able to understand and replicate your issue and provide you with a much concise answer. – NightEye Aug 04 '21 at 19:00
  • Sure thing. Here's an edited example of the sheet. https://docs.google.com/spreadsheets/d/1nJ5brjmx1Bl9Z5_u2TFDOSfNwUeeFXp5wLTgR0tkcgs/edit?usp=sharing I havent been able to get anywhere with this whole paste as values thing, so another idea I have is to just move the rows to the bottom once --today() hits -1, then move it to another sheet once it hits -30. That way old data gets out of the way, and is archived only when it is no longer needed for that months calendar. Issue with this is I've only found solutions that use onEdit, so -today() has to be manually edited – Bobsleder Aug 04 '21 at 19:06
  • Hi @Eli, seems like the sheet isn't public. Kindly have it public. – NightEye Aug 04 '21 at 19:10
  • Just changed it, sorry I'm a little new to sheets! – Bobsleder Aug 04 '21 at 19:19
  • Your problem seems very hard to visualize. Can you provide a before and after snapshot of the sheet behavior? It would clarify your goal and easy to replicate. – NightEye Aug 04 '21 at 20:27
  • @NaziA sure thing. Not sure how to add a photo of the comment, so the SS is in sheets. Since the calendar is pulling from the active sheet, and rows on the active sheet go to "archive" when J2-Today() is -1(yesterday), those then disappear from the calendar. I've tried to make a mixed sheet with both archive and active, but I get duplicates as a frequent issue. – Bobsleder Aug 04 '21 at 20:49
  • @NaziA my solution right now is to just move these rows to the very bottom of the sheet so that they're out of the way, but still stay on. I'll just have to manually copy and paste these over at the end of the month. If you have any suggestions as to how to just keep values on the calendar without copy and pasting every time or anything else let me know – Bobsleder Aug 04 '21 at 20:58
  • edit your original post and include your photos there. comments are only limited to text. – NightEye Aug 04 '21 at 21:10
  • @NaziA Done, thanks for the tip – Bobsleder Aug 04 '21 at 22:14

1 Answers1

0

See code below:

function archiveRows() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var mainSheet = ss.getSheetByName("Sheet1");
  var archiveSheet = ss.getSheetByName("Sheet2");

  // last row should be on the data itself, same with column
  var lastRow = mainSheet.getLastRow(); 
  var lastCol = mainSheet.getLastColumn(); 
  // set time to 0 so we can only compare dates
  var dateToday = new Date().setHours(0, 0, 0, 0);

  var range = mainSheet.getRange(1, 1, lastRow, lastCol);
  var values = range.getValues();
  // remove header to only process data
  var header = values.shift();
  var indices = [], difference = [];

  // remove dates that are earlier than today
  // collect rows and indices data when condition is not met
  values.forEach(function (row, index) {
    if (row[9] >= dateToday)
      return true;
    difference.push(row);
    indices.push(index);
  });

  // remove the row with earlier date
  // offset with 2 due to 0-indexing and header
  indices.forEach(index => mainSheet.deleteRow(index + 2));

  // all rows that were deleted are copied to archive sheet
  difference.forEach(row => archiveSheet.appendRow(row));
}

Sample Data:

sample1 sample2

Output:

output1 output2

Note:

  • If you need to have this run on a daily basis, Time Driven trigger is your friend. Just have it set on a daily basis triggering archiveRows
  • Make sure that the main sheet only contains the data. (I see your sheet has rows containing 9AM values below. Make sure to clear those as the script will mistakenly include it on the processing)
  • Adjust date condition if needed.
NightEye
  • 10,634
  • 2
  • 5
  • 24
  • If that's it, then you would only need to just trigger it once a month. I do recommend running it on the first day (12am) of every month so you don't need to adjust the date condition to where the filter method bases its result. if it is triggered on 12am first day of every month, then it would just remove the previous dates. (e.g. triggered on september 1, 12am. Then all earlier than that including last day of august will be archived.) – NightEye Aug 05 '21 at 16:51
  • You are awesome, thanks! Anyway to change this to be 15 days or more instead of anything less? – Bobsleder Aug 05 '21 at 17:06
  • I'd like to help you further but it is now deviating from your original post. I am also looking into other posts so you have to look it up or post another question in stackoverflow. But a hint is you need to modify the `if (row[9] >= dateToday)` part. It should be relatively easy. Take [this](https://stackoverflow.com/questions/7751936/javascript-date-plus-2-weeks-14-days) as a reference, you need to modify the answer but you should be able to figure it out. – NightEye Aug 05 '21 at 17:34