Struggling beginner here, trying to get my script done and I'm just lost at this point. Let me start by posting my script.
function dailyclear(e)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Today');
sheet.copyTo(ss)
var nusheet = ss.getSheetByName('Copy of Today')
var value = nusheet.getRange("B2").getCell(1,1).getValue()
SpreadsheetApp.setActiveSheet(nusheet)
Logger.log("Current index of sheet: %s", nusheet.getIndex())
ss.moveActiveSheet(2);
nusheet.setName(value);
//Clear "Today"
var lastrow = sheet.getLastRow()
sheet.deleteRows(8, lastrow-7)
//Set Date Value for "Today"
var dd = Utilities.formatDate(new Date(), "GMT-5", "MM/dd/yy");
sheet.getRange("B2").getCell(1,1).setValue(dd);
}
What I want my script to do is before the //Clear line have an If/else statement that goes like this..."IF there is any text in A8 THEN run sheet.deleterows(8, lastrow-7), ELSE skip to setting the date." I hope that makes sense. Basically I want to to delete all rows except the top 7 based off if any data has been entered in A8, otherwise don't try to delete anything because it will just error on me and lock up.
I have been trying for days to figure this out on my own just by reading on the net and I am throwing in the towel. Any help would be appreciated. The link to my sheet is https://docs.google.com/spreadsheets/d/1NZxXjMptx6ldzL2CKk6OwJcq_heIv995ZGVeIE59xNc/edit?usp=sharing
Thanks for anything and everything offered!
I think this is what you're looking to achieve:
function dailyclear() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ui = SpreadsheetApp.getUi();
var sheet = ss.getSheetByName('Today');
var getB2 = sheet.getRange("B2").getValue();
sheet.copyTo(ss).setName(getB2);
var nusheet = ss.getSheetByName(getB2);
Logger.log("Current index of sheet: %s", nusheet.getIndex());
ss.setActiveSheet(nusheet);
ss.moveActiveSheet(2);
var getA8 = sheet.getRange('A8').getValue();
if (getA8) {
var numRows = sheet.getMaxRows();
if (numRows > 8) {
sheet.deleteRows(8, numRows - 8);
} else {
sheet.deleteRow(8);
}
} else {
var newDate = Utilities.formatDate(new Date(), "GMT-5", "MM/dd/yy");
sheet.getRange("B2").setValue(newDate);
}
}
Related
My If statement says that if the cell it is looking at is null, then return x, if it's got a value in it, then Y... The logger shows data in the cell, but it still returns x!
I've tried changing to isblank, is null, is "". I can see that it is logging data, but it is still returning the answer for a blank cell
function trackingSheetUpdate() {
var now = new Date();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName('Active Campaign');
var Tracking = ss.getSheetByName('Tracking')
var getUpdate = sourceSheet.getRange(2, 5).getValue();
var dateResult = now - sourceSheet.getRange(2, 6).getValue();
var openSS = SpreadsheetApp.openByUrl(getUpdate).getSheetByName('Sheet1')
var weeklyUpdate = openSS.getRange(2, 1).getValue();
if (dateResult > 1 && dateResult <= 7); {
if (weeklyUpdate == null, Tracking.getRange(2, 2).setValue("x"));
else
("Y");
}
}
I want x if the cell is blank and Y if cell is not blank, but it only returns x! Eventually I am going to loop this through a dynamic range, but want to get the basics right first!
I created some test sheet data of my own and the below code seemed to work as you're expecting it to.
function trackingSheetUpdate() {
var now = new Date();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName('Active Campaign');
var trackSheet = ss.getSheetByName('Tracking');
var getUpdate = sourceSheet.getRange(2, 5).getValue();
var dateResult = now - sourceSheet.getRange(2, 6).getValue();
var openSheet = SpreadsheetApp.openByUrl(getUpdate).getSheetByName('Sheet1');
var weeklyUpdate = openSheet.getRange(2, 1).getValue();
if (dateResult > 1 && dateResult <= 7); {
if (weeklyUpdate == '') {
trackSheet.getRange(2, 2).setValue("x");
} else {
trackSheet.getRange(2, 2).setValue("y");
}
}
}
Of course this relies on your sheet data being correct, for example you need var getUpdate to be a URL. I have also renamed some variables for consistency with the rest of the script, along with some grammatical changes like making sure there are semicolons to end lines etc.
I have been working on a google sheet that that receives a form submission.
Once the submission comes in I need a script to move the rows of data to different tabs depending on the name shown in column C.
I have extensively searched for solutions on stack overflow and I am very close to a solution right now through the code I have found and edited.
I have a script that will move historic dates to a different tab and leave all future dates in the original tab based in the logic of the date being older than today.
All I need to do now is modify this to move the rows with the name "John" in column C to the John tab and ignore the date.
Once I can get one name to work I am confident I can make this work for multiple names and multiple tabs.
Please feel free to create a copy of the following test sheet I have been working on.
link:
https://docs.google.com/spreadsheets/d/1zJpylrD_5hzScW3lIjIQQSKiY0Aan6Wkm_h_IbVrVXM/edit#gid=0
function MovePastDates() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var entrySheet = ss.getSheetByName("Entry Sheet");
var franksSheet = ss.getSheetByName("Franks Sheet");
var lastColumn = entrySheet.getLastColumn();
for(var i = entrySheet.getLastRow(); i > 0; i--){
var dateCell = entrySheet.getRange(i, 1).getValue();
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 = entrySheet.getRange(i, 1, 1, entrySheet.getLastColumn()).getValues();
franksSheet.getRange(franksSheet.getLastRow() + 1, 1, 1, entrySheet.getLastColumn()).setValues(rangeToMove);
entrySheet.deleteRow(i);
}
}
}
The final result should be a google sheet that receives form entries.
Each entry will be allocated to a specific person who will only have edit permissions to there own tab only where they can approve/decline requests submitted through the form.
All other users of the sheet will have view only access.
I wanted two scripts:
1) Script to move form submission rows to a specific tab dependent on person’s name
(I was going to set up a trigger every minute for this)
2) Script to move past dates into an historic sheet
(I was going to set up a trigger every night for this)
I have been able to modify your code to achieve the desired function, it may not be the most efficient but it appears to work well.
Script One is:
function moveRowsToNamesSheets() { //Name of function
var sObj={John:'Johns Sheet',Frank:'Franks Sheet',David:'Davids Sheet'}; // Put key work and sheet name here in format eg.( keyWord1: 'sheet name to move keyWord1 to')
var ss=SpreadsheetApp.getActive(); // ??
var esh=ss.getSheetByName('Entry Sheet'); //Sheet data is being pulled form
var fsh=ss.getSheetByName('Franks Sheet'); //unsure why one of the sheets is named here
var erg=esh.getDataRange(); // Not sure of function now that I am not using dates
var evA=erg.getValues(); // ??
var d=0; //??
//var today=new Date(new Date().getFullYear(),new Date().getMonth(),new Date().getDate()).valueOf(); // Didnt need this line
for(var i=1;i<evA.length;i++) { //??
if(/*new Date(evA[i][0]).valueOf() < today*/ evA[i][2]=='John' ||evA[i][2]=='Frank' ||evA[i][2]=='David') { //Keywords used go here, what does the [2] mean?
ss.getSheetByName(sObj[evA[i][2]]).appendRow(evA[i]); //??
esh.deleteRow(i+1-d);
d++; //increments d by one
}
}
}
Script Two is:
function HistoricDates() {
// Initialising
var ss = SpreadsheetApp.getActiveSpreadsheet();
//--------------- Franks Sheets --------------------
var franksSheet = ss.getSheetByName("Franks Sheet");
var PastSheet = ss.getSheetByName("Historic Requests");
var lastColumn = franksSheet.getLastColumn();
// Check all values from your "Franks Sheet" sheet
for(var i = franksSheet.getLastRow(); i > 0; i--){
// Check if the value is a valid date
var dateCell = franksSheet.getRange(i, 4).getValue(); //Dates in column 4
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 = franksSheet.getRange(i, 1, 1, franksSheet.getLastColumn()).getValues();
PastSheet.getRange(PastSheet.getLastRow() + 1, 1, 1, franksSheet.getLastColumn()).setValues(rangeToMove);
franksSheet.deleteRow(i);
}
}
}
//---------------------- Johns Sheets -------------------------
var johnsSheet = ss.getSheetByName("Johns Sheet");
var pastSheet = ss.getSheetByName("Historic Requests");
var lastColumn = johnsSheet.getLastColumn();
// Check all values from your "Johns Sheet" sheet
for(var i = johnsSheet.getLastRow(); i > 0; i--){
// Check if the value is a valid date
var dateCell = johnsSheet.getRange(i, 4).getValue(); //Dates in column 4
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 = johnsSheet.getRange(i, 1, 1, johnsSheet.getLastColumn()).getValues();
pastSheet.getRange(pastSheet.getLastRow() + 1, 1, 1, johnsSheet.getLastColumn()).setValues(rangeToMove);
johnsSheet.deleteRow(i);
}
}
}
//--------------- Davids Sheets --------------------
var davidsSheet = ss.getSheetByName("Davids Sheet");
var pastSheet = ss.getSheetByName("Historic Requests");
var lastColumn = davidsSheet.getLastColumn();
// Check all values from your "Davids Sheet" sheet
for(var i = davidsSheet.getLastRow(); i > 0; i--){
// Check if the value is a valid date
var dateCell = davidsSheet.getRange(i, 4).getValue();//Dates in column 4
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 = davidsSheet.getRange(i, 1, 1, davidsSheet.getLastColumn()).getValues();
pastSheet.getRange(pastSheet.getLastRow() + 1, 1, 1, davidsSheet.getLastColumn()).setValues(rangeToMove);
davidsSheet.deleteRow(i);
}
}
}
}
// Check is a valid date
function isValidDate(value) {
var dateWrapper = new Date(value);
return !isNaN(dateWrapper.getDate());
}
The working spreadsheet is located here:
https://docs.google.com/spreadsheets/d/1VCONRkBpkva-KrFDO2bFV8ZTp1U168QWAGavcKCa_uQ/edit?usp=sharing
I think this is what you want:
function movePastDatesOrJohn() {
var sObj={John:'Johns Sheet',Frank:'Franks Sheet',David:'Davids Sheet'};
var ss=SpreadsheetApp.getActive();
var esh=ss.getSheetByName('Entry Sheet');
var fsh=ss.getSheetByName('Franks Sheet');
var erg=esh.getDataRange();
var evA=erg.getValues();
var d=0;
var today=new Date(new Date().getFullYear(),new Date().getMonth(),new Date().getDate()).valueOf();
for(var i=1;i<evA.length;i++) {
if(new Date(evA[i][0]).valueOf() < today || evA[i][2]=='John') {
ss.getSheetByName(sObj[evA[i][2]]).appendRow(evA[i]);
esh.deleteRow(i+1-d);
d++;
}
}
}
So Franks Sheet and Davids Sheet only get the rows that are older than today. But Johns Sheet gets all of the row that are Johns and disregards the date. I think that's what you wanted.
By the way, did you know that if you more that one form attached to your spreadsheet you can tell which response sheet the formSubmit trigger is writing too, with the event object range? Using sheet name = e.range.getSheet().getName();
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);
I'm new to Google App Script and new to coding for Google Sheets.
I'm attempting to:
Have a pop-up box show if any date in a range of cells is equal to today.
Here's the code I have so far:
function onOpen()
{
var ss = SpreadsheetApp.getActiveSheet();
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Required Items List');
var ss = SpreadsheetApp.getActive();
var range = sheet.getRange('Required Items List!E8:G22');
var data = range.getValue();
var today = new Date();
Logger.log(data);
Logger.log(today);
if (data == today)
{
Browser.msgBox('Send Required Items Reminders Today!', Browser.Buttons.OK);
}
else {}
}
Range Example
It appears by data variable on reads the first cell for E8. I'm sure its a simple solution, I'm just missing some logic for it. Any help would be greatly appreciated.
You may code something like this :
function onOpen(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var lastrow = ss.getLastRow();
var lastcol = ss.getLastColumn();
var today = Utilities.formatDate(new Date(), "GMT+05:30", "''yyyy-MM-dd");
var flag = 'false';
for(var i =8; i<=22; i++){
for(var j =5; j<=7; j++){
var data = sheet.getRange(i, j, i, j).getValue();
var shDate = Utilities.formatDate(new Date(data), "GMT+05:30", "''yyyy-MM-dd");
if (shDate == today){
Browser.msgBox('Send Required Items Reminders Today!', Browser.Buttons.OK);
flag = 'true';}
if(flag == 'true')
break;
}
if(flag == 'true')
break;
}
}
Hope this will help you.
Thanks.
I am working in a google document spreadsheet to make a list of active customers. I am attempting to create a script that when a completion date is entered, that row (all the customer info) is transfered to another sheet and/or deleted. I started a script but it isn't working properly.
function myFunction() {
'var ssdate = spreadsheet.column B'
// the "if" clause : if (date = 1) {
// the "then" clause : then ( move row to sheet 2)
}
Since this is my first time, I am sure this needs some work, but any help would be greatly appreciated.
It's much easier to read the Google Apps Script tutorials and guide first. They will give you a much better quick start.
Please try the following example:
var ddate = Utilities.formatDate(new Date(), "GMT", "MM-dd-yy");
function checkDate(){
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var rows = sheet.getDataRange();
var rowsNumber = rows.getNumRows();
var column = sheet.getDataRange();
var values = column.getValues();
for (var i = 1; i <= rowsNumber -1; i++){
var customerData = values[i];
var customerDate = customerData[2];
var myRow = Math.round(i) +1;
var conCustomerDate = Utilities.formatDate(new Date(customerDate), "GMT", "MM-dd-yy");
if (conCustomerDate > ddate){
sheet.getRange("D" + myRow).setValue("OK");
}
else {
sheet.getRange("D" + myRow).setValue("Old");
}
}
}