I currently have a script on Google Sheets that auto deletes rows after a certain date. However, I would like to change it so that it auto archives the rows to another sheet instead. I added in the target sheet , but I'm at a loss on how to move the rows, before it deletes.
function ArchiveOldEntries() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Form Responses 1");
var datarange = sheet.getDataRange();
var lastrow = datarange.getLastRow();
var values = datarange.getValues();
var targetSheet = ss.getSheetByName("Archive");
var currentDate = new Date()//today
var yesterday = new Date();//yesterday
yesterday.setDate(currentDate.getDate() - 1);
for (i=lastrow;i>=2;i--) {
var tempDate = values[i-1][2];
if (tempDate <= yesterday)
{
sheet.deleteRow(i)
}
}
}
Archive and Delete rows one day old or more
function ArchiveOldEntries() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet0");//source sheet
const vs = sh.getDataRange().getValues();
const tsh = ss.getSheetByName("Sheet1");//archive sheet
const dt = new Date();
const ytdv = new Date(dt.getFullYear(), dt.getMonth(), dt.getDate() - 1).valueOf();//yesterday value
let d = 0;
vs.forEach((r,i) => {
let cdv = new Date(r[2]).valueOf();//date is in column3
if(cdv <= ytdv) {
tsh.getRange(tsh.getLastRow() + 1,1,1,r.length).setValues([r])
sh.deleteRow(i + 1 - d++)
}
});
}
Related
I have a sheet with 6 columns that is being updated with a webhook using the doPost function below that works just fine.
I have filtered only the rows where column 5 = 'true' (index [4]), but I would like to move all columns that match this condition of true in column 5 into another sheet and then clear the first sheet leaving only the header rows in the first sheet.
I'm nearly there, but I would appreciate it if anyone has any suggestions to achieve this final step. Thanks for reading...
function doPost(e) {
var jsonString = e.postData.getDataAsString();
var event = JSON.parse(jsonString)
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Import");
var timeStamp = new Date();
var time = Utilities.formatDate(timeStamp, "GMT+1", "dd/MM/yyyy, h:mm a");
var nextRow = sheet.getLastRow()+1;
//Insert the data into the sheet
sheet.getRange(nextRow, 1).setValue(time);
sheet.getRange(nextRow, 3).setValue(jsonString);
sheet.getRange(nextRow, 4).setValue(event.data.amount);
sheet.getRange(nextRow, 5).setValue(event.data.metadata.is_supplementary);
sheet.getRange(nextRow, 6).setValue(event.data.reference);
}
function getData() {
var rows = SpreadsheetApp.getActiveSheet().getDataRange().getValues();
var filteredRows = rows.filter(function(row){
var is_supplementary = row[4];
if (is_supplementary == true){
return row;
}
})
Logger.log(filteredRows);
}
Try this:
function doPost(e) {
const jsonString = e.postData.getDataAsString();
const event = JSON.parse(jsonString)
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sh = ss.getSheetByName("Import");
const ts = new Date();
const time = Utilities.formatDate(ts, "GMT+1", "dd/MM/yyyy, h:mm a");
const nextRow = sh.getLastRow() + 1;
sh.getRange(nextRow, 1).setValue(time);
sh.getRange(nextRow, 3, 1, 4).setValues([[jsonString, event.data.amount, event.data.is_supplementary, event.data.reference]]);//doing last 3 all at once
}
function getData() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getActiveSheet();//getting active sheet
const vs = sh.getRange(2,1,sh.getLastRow() - 1,sh.getLastColumn()).getValues().filter(r => r[4]);//test truthiness
const osh = ss.getSheetByName("output");
osh.getRange(osh.getLastRow() + 1, 1, vs.length,vs[0].length).setValues(vs);
sh.getRange(2,1,sh.getLastRow() - 1, sh.getLastColumn()).clearContent();
}
My script is working but it times out. I am unsure as to what I can do to keep it from doing this.
function archived(e) {
var sheet = SpreadsheetApp.getActive().getSheetByName('IVA');
const targetSheet = e.source.getSheetByName('Archived Videos');
const numColumnsToMove = 20;
var sheetTo = SpreadsheetApp.getActive().getSheetByName('IVA');
var myValues = sheetTo.getRange('A:A').getValues();
var i;
for (i = 0; i < myValues.length; i++) {
if (myValues[i][0] === '')
return i + 1;
const rangeToMove = sheet.getRange(e.range.rowStart, 1, 1, numColumnsToMove);
const values = rangeToMove.getValues();
appendRowsV(targetSheet, values, 1);
sheet.deleteRows(e.range.rowStart, 1);
var lRow = sheet.getLastRow();
var lCol = sheet.getLastColumn(), range = sheet.getRange(lRow,1,1,lCol);
sheet.insertRowsAfter(lRow, 1);
range.copyTo(sheet.getRange(lRow+1, 1, 1, lCol), {contentsOnly:false});
}
}
As an FYI my sheet has 1500 rows and 15 columns. don't know if that is important or not.
Just in case. As far as I can tell from your code you're trying to append all data from the sheet 'IVA' at the bottom of the sheet 'Archived Videos'. It can be done this way:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var src_range = ss.getSheetByName('IVA').getDataRange();
var dest_sheet = ss.getSheetByName('Archived Videos');
var last_row = dest_sheet.getLastRow() + 1;
var dest_range = dest_sheet.getRange('A' + last_row);
src_range.copyTo(dest_range);
}
I'm not good with coding and i tried to mix some codes that i found and made what i needed, but now i face a difficult problem for me, so I'm trying when to press the button that i created to run the script to send the data that have for example an 'X' in the last column. i successfully made the sending from one sheet to another but it send all the data for now i want to be sent only the chosen ones with an 'X'.
any help I'll be grateful.
this is my code
thank you
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Sheet1");
var url = "https://docs.google.com/spreadsheets/d/15nIAXcP0a14OvvBr5lww4tO23stQD4PEu0QDAcgaFyE/edit#gid=0";
var ss2 = SpreadsheetApp.openByUrl(url);
var pasteSheet = ss2.getSheetByName("Sheet2");
// get source range
var max = copySheet.getMaxRows().toString();
var range = copySheet.getRange(2,1,max,4);
var dataValues = range.getValues();
for(var i = 1; i < dataValues.length; i++)
{
if(dataValues[i][5] === 'X')
{
copySheet.appendRow([dataValues[i][0],
dataValues[i][1],
dataValues[i][2],
dataValues[i][3],
dataValues[i][4]]);
}
}
for(var i = 1; i < dataValues.length; i++)
{
if(dataValues[i][5] === 'X')
{
var clearRow = i+1;
dataSheet.getRange('A' + clearRow + ':F' + clearRow).clear();
}
}
// get destination range
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,max,1);
// clear source values
Browser.msgBox('Commande Confirmer');
}
This is all that your function does
dataSheet is undefined so I assume it was csh
function lfunko() {
const ss = SpreadsheetApp.getActive();
const csh = ss.getSheetByName("Sheet1");
const ss2 = SpreadsheetApp.openByUrl("ssurl");
const psh = ss2.getSheetByName("Sheet2");
var vs = csh.getRange(2, 1, csh.getLastRow() - 1, csh.getLastColumn()).getValues();
vs.forEach((r,i) => {
if (vs[i][5] === 'X') {
csh.appendRow([vs[i][0], vs[i][1], vs[i][2], vs[i][3], vs[i][4]]);
csh.getRange(i + 1, 1, 1, 6).clear();
}
});
}
I tried to fill it with my sheet data, but that
var dataRange = sheet.getDataRange();
get an error. What should I do?
function RefreshImports() {
var lock = LockService.getScriptLock();
if (!lock.tryLock(5000)) return; // Wait up to 5s for previous refresh to end.
var id = "1r7TgCWXfjmcjufT0yz9qcFDlr482SdHDHlZYblXyAt0"; // [YOUR SPREADSHEET ID]
var ss = SpreadsheetApp.openById(id);
var sheet = ss.getSheetByName("crypto"); // sheet name
var dataRange = sheet.getDataRange();
var formulas = dataRange.getFormulas();
var content = "";
var now = new Date();
var time = now.getTime();
var re = /.*[^a-z0-9]import(?:xml|data|feed|html|range)\(.*/gi;
var re2 = /((\?|&)(update=[0-9]*))/gi;
var re3 = /(",)/gi;
for (var row=0; row<formulas.length; row++) {
for (var col=0; col<formulas[0].length; col++) {
content = formulas[row][col];
if (content != "") {
var match = content.search(re);
if (match !== -1 ) {
// import function is used in this cell
var updatedContent = content.toString().replace(re2,"$2update=" + time);
if (updatedContent == content) {
// No querystring exists yet in url
updatedContent = content.toString().replace(re3,"?update=" + time + "$1");
}
// Update url in formula with querystring param
sheet.getRange(row+1, col+1).setFormula(updatedContent);
}
}
}
}
// Done refresh; release the lock.
lock.releaseLock();
// Show last updated time on sheet somewhere
sheet.getRange(7,2).setValue("Rates were last updated at " + now.toLocaleTimeString())
}
I set up the trigger.
Not sure what you trying to do and you explanation leaves a lot to be desired.
So this is a simple way to put your formulas into a sheet named "Destination".
function RefreshImports() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("crypto");
const rg = sh.getDataRange();
const formulas = rg.getFormulas();
sh.getRange(1,1,formulas.length,formulas[0].length).setFormulas(formulas);
}
Quite new to JS, I'm willing to do some repetitive task within google sheet script editor.
The idea is to check all rows of tab1. For each row, if date in column D is older than 5 days prior to today, it transfers the full row to tab2 first empty row and make sure the row doesn't exist anymore in tab1. The idea is to archive data older than 5 days.
I could do this with VBA but I am very new to google sheet script and need the help of the community (I'm afraid I really don't have the basis of JS).
I will then add a time trigger (I know how to do this !) for this script.
EDIT:
from this script deleting :
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Foglio1");
var datarange = sheet.getDataRange();
var lastrow = datarange.getLastRow();
var values = datarange.getValues();// get all data in a 2D array
var currentDate = new Date();
var oneweekago = new Date();
oneweekago.setDate(currentDate.getDate() - 7);
for (i=lastrow;i>=2;i--) {
var tempdate = values[i-1][2];// arrays are 0 indexed so row1 = values[0] and col3 = [2]
if(tempdate < oneweekago) { sheet.deleteRow(i); } } }
And this script moving data :
function moveCols() {
var ss = SpreadsheetApp.getActive();
var sourceSheet = ss.getSheetByName('Sheet1');
var destSheet = ss.getSheetByName('Sheet2');
sourceSheet.getRange('A:A').moveTo(destSheet.getRange('G1'))
sourceSheet.getRange('B:B').moveTo(destSheet.getRange('I1'))
sourceSheet.getRange('C:C').moveTo(destSheet.getRange('L1')) }
I was able to do the below :
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("input");
var destSheet = ss.getSheetByName("input archive");
var datarange = sheet.getDataRange();
var lastrow = datarange.getLastRow();
var values = datarange.getValues();// get all data in a 2D array
var currentDate = new Date();
var oneweekago = new Date();
oneweekago.setDate(currentDate.getDate() - 7);
for (i=lastrow;i>=2;i--) {
var tempdate = values[i-1][2];// arrays are 0 indexed so row1 = values[0] and col3 = [2]
if(tempdate < oneweekago) {
sourceSheet.getRange('i:i').moveTo(destSheet.getRange('LASTAVAILABLEROW'));
} } }
But I have no idea how to paste to last row. Maybe this will work ?
var Avals = destSheet.getRange("A1:A").getValues();
var Alast = Avals.filter(String).length;
Haven't got time to write a full answer but hope this gives you some guidance
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName("data");
var sheet2 = ss.getSheetByName("archive");
var data = sheet1.getRange("I1:I"+sheet.getLastRow()).getValues();
sheet2.getRange("A" + (sheet2.getLastRow()+1) + ":A" +sheet.getLastRow()).setValues(data);