I have a problem in updating the ranges in multiple slicers at once, which is not possible.
I tried using the script below that i found in another thread for automatically deleting and recreating the slicers with the desired range.
but i get the following error. "destinationSheet" not defined.
Would appreciate in you can help me with a solution here.
Thank you.
function test() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Tracking');
var slicers = sheet.getSlicers();
var rowValue = sheet.getLastRow();
var newRange = sheet.getRange('A3:X'+rowValue); //new range
for (i=0; i<slicers.length; i++){
slicers[i].remove();
}
//Slicers to column A and D
destinationSheet.insertSlicer(newRange, 2, 1); // choose anchorRowPos and anchorColPos
destinationSheet.insertSlicer(newRange, 2, 4);
//Get new slicers
var slicers = destinationSheet.getSlicers();
var filterCriteria = SpreadsheetApp.newFilterCriteria().setHiddenValues(['']).build();
slicers[0].setColumnFilterCriteria(1, filterCriteria);
slicers[0].setTitle('Title1');
var filterCriteria2 = SpreadsheetApp.newFilterCriteria().setHiddenValues(['1']).build();
slicers[1].setColumnFilterCriteria(24, filterCriteria2);
slicers[1].setTitle('Title2');
}
Thanks to Tanaike's suggestion to change the "destinationSheet" part of the code to 'sheet', the code writen as below worked.
function test() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Tracking');
var slicers = sheet.getSlicers();
var rowValue = sheet.getLastRow();
var newRange = sheet.getRange('A3:X'); //new range
for (i=0; i<slicers.length; i++){
slicers[i].remove();
}
//Slicers to column A and D
sheet.insertSlicer(newRange, 2, 1); // choose anchorRowPos and anchorColPos
sheet.insertSlicer(newRange, 2, 4);
//Get new slicers
var slicers = sheet.getSlicers();
var filterCriteria = SpreadsheetApp.newFilterCriteria().setHiddenValues(['']).build();
slicers[0].setColumnFilterCriteria(1, filterCriteria);
slicers[0].setTitle('Title1');
var filterCriteria2 = SpreadsheetApp.newFilterCriteria().setHiddenValues(['1']).build();
slicers[1].setColumnFilterCriteria(24, filterCriteria2);
slicers[1].setTitle('Title2');
}
Related
Problem: Cannot update the ranges for two or more slicers with Google Script
Info: The sheet contains two slicers
Code:
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Tracking');
var slicers = sheet.getSlicers();
var slicerRange = sheet.getRange('A3:A160'); //current slicer range A3:A165
slicers[0,1].setRange(slicerRange);
}
Error: Two slicers can either have zero or all common rows.
What is wrong? The code works if I only work with one slicer in the sheet and code.
Thanks so much in advance for any help!
I dont think you can edit their range.
But you could probably remove and create them again with your new range:
function test(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Tracking');
var slicers = sheet.getSlicers();
var newRange = sheet.getRange('A3:A160'); // your new range
var slicersCount = 0;
for (i=0;i<slicers.length;i++){
slicers[i].remove();
slicersCount ++;
}
for (i=0;i<slicersCount;i++){
sheet.insertSlicer(newRange, 5, 3); // choose anchorRowPos and anchorColPos you want
}
}
And then you can modify the rest of their settings as you wish.
Hope this helps.
Thanks a lot for your help! It eventually helped me to make it work.
This is the code I ended up with, which is without counter on the insertSlicer() since I need to give individual settings:
function test() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Tracking');
var slicers = sheet.getSlicers();
var rowValue = sheet.getLastRow();
var newRange = sheet.getRange('A3:X'+rowValue); //new range
for (i=0; i<slicers.length; i++){
slicers[i].remove();
}
//Slicers to column A and D
destinationSheet.insertSlicer(newRange, 2, 1); // choose anchorRowPos and anchorColPos
destinationSheet.insertSlicer(newRange, 2, 4);
//Get new slicers
var slicers = destinationSheet.getSlicers();
var filterCriteria = SpreadsheetApp.newFilterCriteria().setHiddenValues(['']).build();
slicers[0].setColumnFilterCriteria(1, filterCriteria);
slicers[0].setTitle('Title1');
var filterCriteria2 = SpreadsheetApp.newFilterCriteria().setHiddenValues(['1']).build();
slicers[1].setColumnFilterCriteria(24, filterCriteria2);
slicers[1].setTitle('Title2');
}
I want to copy data from one sheet (Sheet1) to another (Sheet2). Before the data copies over, I want to check in column A on both sheets for duplicates, so that only the non-duplicates are copied over. I am specifically having trouble writing the IF statement and FOR loop to do this.
I have the following code and pictures below:
function Demo() {
var Sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var Sheet2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2");
var ColumntoSearch = 1;
var LastRow = Sheet1.getLastRow();
var LastRow2 = Sheet2.getLastRow();
//Gets range and values to compare for duplicates
var Range = Sheet1.getRange(2, ColumntoSearch,LastRow, 1);
var Values = Range.getValues();
logger.log("The Values to transfer over are: " + Values);
//Gets range and values to compare duplicates to
var Range2 = Sheet2.getRange(5, 1, LastRow2, 1)
var Values2 = Range2.getValues();
logger.log("The Values to Search are: " + Values2);
//Sets the amount of data to copy over
var NumberofColumns = 2;
var NumberofRows = 1;
var DestRow = Sheet2.getLastRow()+1;
//Compares all the material codes for duplicates and copies over the non-duplicates
var dt=new Date();
var dv=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();
var d=0;
for(var i=0;i<Values.length;i++) {
if((???????)) {
var RangetoCopy=Sheet1.getRange(i-d+5,1,NumberofRows,NumberofColumns);
var DestRange=Sheet2.getRange(Sheet2.getLastRow()+1,1,NumberofRows,NumberofColumns);
RangetoCopy.copyTo(DestRange);
d++;}}
Logger.log("Next Destination Row: " + DestRow);
}
Image1
Try something like this:
function checkForDupesAndCopy() {
var ss=SpreadsheetApp.getActive();
var sh1=ss.getSheetByName("Sheet1");
var sh2=ss.getSheetByName("Sheet2");
var ColumntoSearch = 1;
var rg1=sh1.getRange(2,1,sh1.getLastRow()-1,1); //cola sheet1
var vA1=rg1.getValues();
var rg2=sh2.getRange(5,1,sh2.getLastRow()-4,1)
var vA2=rg2.getValues();
var vlist=vA2.map(function(r){return r[0];});
var dt=new Date();
var dv=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();
for(var i=0;i<vA1.length;i++) {
if(vlist.indexOf(vA1[i][0])==-1) {
var RangetoCopy=sh1.getRange(i+2,1,1,2);
var DestRange=sh2.getRange(sh2.getLastRow()+1,1,1,2);
RangetoCopy.copyTo(DestRange);
}
}
}
Note: I tested this on some data of my own.
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 am very new on using Google Apps Script and has a shallow knowledge on programming. What I am trying to do is copy the values of specific columns to a different Spreadsheet. Here's my code:
function myFunction() {
var ss = SpreadsheetApp.getActive();
var responses = ss.getSheetByName("Responses ID");//Where ID's of the spreadsheets are listed.
var consolidatedSheet = ss.getSheetByName("Consolidated");//Where the data should be pasted.
var responseColValues = responses.getRange(2,2, responses.getMaxRows() -1).getValues();
var responsesIds = [i for each (i in responseColValues)if (isNaN(i))];
var ssSelected = SpreadsheetApp.openById(responsesIds[0]);
var selectedSheets = ssSelected.getSheets();
for (i=0; i<3; i++){
var maxRows = selectedSheets[i].getLastRow()-1;
var x=2, y=2;
var lastRow = consolidatedSheet.getLastRow()+1;
for (j=0; j<maxRows; j++){
var eventID = selectedSheets[i].getRange(y,2).getValue();
var employeeName = selectedSheets[i].getRange(y,3).getValue();
var productionDate = selectedSheets[i].getRange(y,4).getValue();
var consolidatedSheetCell = consolidatedSheet.getRange(lastRow,1).setValue(eventID);
var consolidatedSheetCell = consolidatedSheet.getRange(lastRow,2).setValue(employeeName);
var consolidatedSheetCell = consolidatedSheet.getRange(lastRow,3).setValue(productionDate);
y++;
lastRow++;
}
}
}
However, I am experiencing this notification on the Execution hints (light bulb icon):
screenshot of the message. I am thinking that my code can be simplified. I am just not sure how to do it. Thank you in advance.
Every line with .getRange().getValue() and getRange.setValue() is a call to the file. Since you have these inside a for(){} loop, they are being called many times. Your goal is to limit these to as few as possible. Since you can read and write a range, you could do something similar to this:
function myFunction() {
var ss = SpreadsheetApp.getActive();
var responses = ss.getSheetByName("Responses ID");//Where ID's of the spreadsheets are listed.
var consolidatedSheet = ss.getSheetByName("Consolidated");//Where the data should be pasted.
var responseColValues = responses.getRange(2,2, responses.getMaxRows() -1).getValues();
var responsesIds = [i for each (i in responseColValues)if (isNaN(i))];
var ssSelected = SpreadsheetApp.openById(responsesIds[0]);
var selectedSheets = ssSelected.getSheets();
for (i=0; i<3; i++){
var maxRows = selectedSheets[i].getLastRow()-1;
var y=2;
var lastRow = consolidatedSheet.getLastRow()+1;
var copyValues = selectedSheets[i].getRange(y,2, maxRows, 4).getValues();
consolidatedSheet.getRange(lastRow,1, maxRows, 4).setValues(copyValues);
}
}
I am trying to get a chart from spreadsheet A. I take this into a Chart object, and try to put it back in spreadsheet B.
I always get an 'unknown sheetname' error. I have been trying to put new ranges in the new chart, as you can see in the code. When I do a Logger.log(newrange.getSheet()) I indeed get Sheet instead of SHEETNAME, like the chart builder messes with the sheetnames.
What is happening?
function updateCharts() {
var ss = SpreadsheetApp.openById("ID");
var sheet = ss.getSheetByName("SHEETNAME");
var charts = sheet.getCharts();
var ss2 = SpreadsheetApp.openById("NEW ID");
var sheet2 = ss2.getSheetByName("SHEETNAME");
for (var c=0; c<charts.length; c++) {
var chart = charts[c];
var ranges = chart.getRanges();
var type = chart.getType();
var info = chart.getOptions();
var builder = chart.modify();
var range = ranges[0];
builder.removeRange(range);
var startRow = range.getRow();
var startCol = range.getColumn();
var numRows = range.getNumRows();
var numCols = range.getNumColumns();
var newrange = ss2.getSheetByName("statistiek").getRange(startRow, startCol, numRows, numCols);
Logger.log(newrange.getSheet());
builder.addRange(newrange);
var title = chart.getOptions().get('title')
builder.setOption('title', title + " " + p);
var newchart = builder.build();
ss2.getSheetByName("statistiek").insertChart(newchart);
}
}
Logger.log() requires a string, but you're feeding it an object with newrange.getSheet(). Try this instead, if you want the sheet's name:
Logger.log(newrange.getSheet().getName())
To see the names of all sheets in a spreadsheet, try:
var ss = SpreadsheetApp.openById("ID");
var allSheets = ss.getSheets();
for (i in allSheets) {
Logger.log("Sheet " + i + " is named " + allSheets[i].getName());
}