Let's say I run a script to extract last 30 days of data from a source (a faebook…
- User
Let's say I run a script to extract last 30 days of data from a source (a faebook fanpage values for videos we post), I update this daily but I want to keep as well data i don't extract anymore after the day 30 of the data. How can I accomplish this? Keep a historic with the older data and the daily extracted (also avoid duplicated items, keeping the highest value item)
- Assistant
One approach to accomplish this is to use a database to store the data and update it daily with the latest data extracted from the source. The database can also keep a historic of the older data.
To avoid duplicated items, you can use a unique identifier for each item, such as the post ID or video ID. When you extract new data, you can check if the item already exists in the database by comparing the unique identifier. If it does exist, you can compare the values of the item in the database with the newly extracted item and keep the one with the highest value.
To keep only the data for the last 30 days, you can use a timestamp or date field in the database to record when each item was extracted. When you extract new data, you can add the current date to this field. You can then query the database to retrieve only the data that was extracted in the last 30 days by filtering on the timestamp or date field.
Overall, using a database allows you to efficiently store and manage large amounts of data, and to perform complex queries to extract and analyze the data as needed. There are many databases available, such as MySQL, PostgreSQL, and MongoDB, among others, that you can choose from depending on your specific requirements and preferences.
- User
I want to do this in google spreadsheets, i have this script you showed me before:
function archiveData() { var sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1'); var archiveSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Archive'); var lastRow = archiveSheet.getLastRow() + 1; var sourceData = sheet1.getDataRange().getValues(); var archiveData = []; var today = new Date(); for (var i = 1; i < sourceData.length; i++) { var existingRow = archiveSheet.getRange(2, 2, archiveSheet.getLastRow() - 1, sourceData[i].length).getValues().findIndex(function(row) { return row[0].getTime() === today.getTime() && row[1] === sourceData[i][1]; // Assumes date is in first column of Archive sheet }); if (existingRow === -1) { archiveSheet.appendRow([today, ...sourceData[i]]); } else { var existingValue = archiveSheet.getRange(existingRow + 2, sourceData[i].length + 1).getValue(); if (sourceData[i][sourceData[i].length - 1] > existingValue) { archiveSheet.deleteRow(existingRow + 2); archiveSheet.appendRow([today, ...sourceData[i]]); } } } } This script works in the same way as the previous script, but instead of updating the value in place, it deletes the existing row with the lower value and appends a new row with the higher value. This should be more efficient because it avoids updating the value in place, which can be slow for large data sets.
- Assistant
Great, since you want to use Google Sheets, you can modify the script to work with Google Sheets instead of a database. Here's an updated version of the script that should work:
scss - User
can you add further comments in the code you provide?
- Assistant
Sure, here's the updated code with additional comments:
- User
Will this replace or delete older rows thata re duplicated with lower values in a column?
- Assistant
Yes, this script will replace or delete older rows that are duplicated with lower values in a column.
The script first checks if a row with the same unique identifier (in this case, the video ID) already exists in the archive sheet. If it does not exist, the script appends the row to the archive sheet. If it does exist, the script compares the value of the last column of the existing row (which is assumed to be the view count) with the value of the last column of the current row from the source sheet. If the current row has a higher view count, the script deletes the existing row and appends the current row to the archive sheet. This ensures that only the row with the highest view count is kept in the archive sheet.
If you want to keep all rows with the same unique identifier, even if they have lower values in a certain column, you can modify the script to append the current row to the archive sheet even if an existing row with the same unique identifier already exists.