remove filtration for Google spreadsheet using script - google-apps-script

I want to remove the filtration from the spreadsheet using a script and after i remove filtered rows the sheet display all row in sheet
I want to do that when I open the spreardsheet.
function ShowColumns() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var maxRows = sheet.getMaxRows();
var maxCol = sheet.getMaxColumns();
sheet.showRows(1, maxRows);
sheet.showColumns(1,maxCol)
}
I searched for it intensively, but did't find anything.

There is a way to do it with Google advanced service - Google Sheets API. And here is my function to turn off the filter in Google Sheet.
function removeFilter(sheet){
var ss = SpreadsheetApp.getActive();
var spreadsheetId = ss.getId();
var sheetId = sheet.getSheetId();
var resource = {
"requests": [
{
"clearBasicFilter": {
"sheetId": sheetId
}
}
]
}
Sheets.Spreadsheets.batchUpdate(resource, spreadsheetId);
}

Related

Copy from template to rangelist

function FunctionC12C31() {
var allSheetTabs,i,L,thisSheet,thisSheetName,sheetsToExclude,value;
sheetsToExclude = ['Template','Sheet1','Sheet2'];
var ss = SpreadsheetApp.getActiveSpreadsheet();
allSheetTabs = ss.getSheets();
L = allSheetTabs.length;
for (i=0;i<L;i++) {
thisSheet = allSheetTabs[i];
thisSheetName = thisSheet.getName();
//continue to loop if this sheet is one to exclude
if (sheetsToExclude.indexOf(thisSheetName) !== -1) {continue;}
value = thisSheet.getRangeList(['C12:C35','C5:C6','G16:G19','G4']).clearContent();
}}
Need help modifying this to instead of clearing contents
it will copy the formula from a RangeList(['C12:C35','C5:C6','G16:G19','G4'])
to be copied to from a Template Sheet too all sheet except to "sheetsToExclude" list.
or perhaps a different script?
copy from a template sheet RangeList(['C12:C35','C5:C6','G16:G19','G4'])
data will be formulas and need to be pasted on same RangeList
paste data to ALL sheets except from specified sheets. Like sheetsToExclude = ['Template','Sheet1','Sheet2'];
I believe your goal is as follows.
You want to retrieve the formulas from the cells 'C12:C35','C5:C6','G16:G19','G4' of a template sheet. And, you want to put the retrieved formulas to the same ranges in the sheets except for sheetsToExclude = ['Template','Sheet1','Sheet2'] in the active Spreadsheet.
The template sheet is included in the same active Spreadsheet. The sheet name is "Template".
In this case, how about the following modification?
Modified script 1:
Unfortunately, in the current stage, the values cannot be retrieved and put from the discrete cells using RangeList. So, in this case, it is required to use another method. In order to retrieve the formulas from the discrete cells and put the formulas to the discrete cells with the row process cost, I would like to propose using Sheets API. So, before you use this script, please enable Sheets API at Advanced Google services.
In this script, "Method: spreadsheets.batchUpdate" is used.
function myFunction() {
// These variables are from your showing script.
var templateSheetName = "Template";
var sheetsToExclude = [templateSheetName, 'Sheet1', 'Sheet2'];
var ranges = ['C12:C35', 'C5:C6', 'G16:G19', 'G4'];
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ssId = ss.getId();
var template = ss.getSheetByName(templateSheetName);
var sheetId = template.getSheetId();
var gridRanges = ranges.map(r => {
var range = template.getRange(r);
var row = range.getRow();
var col = range.getColumn();
return {
sheetId,
startRowIndex: row - 1,
endRowIndex: row - 1 + range.getNumRows(),
startColumnIndex: col - 1,
endColumnIndex: col - 1 + range.getNumColumns(),
};
});
var requests = ss.getSheets().reduce((ar, s) => {
if (!sheetsToExclude.includes(s.getSheetName())) {
gridRanges.forEach(source => {
var destination = JSON.parse(JSON.stringify(source));
destination.sheetId = s.getSheetId();
ar.push({ copyPaste: { source, destination, pasteType: "PASTE_FORMULA" } });
});
}
return ar;
}, []);
Sheets.Spreadsheets.batchUpdate({ requests }, ssId);
}
When this script is run, the formulas are retrieved from 'C12:C35', 'C5:C6', 'G16:G19', 'G4' of templateSheetName sheet, and the retrieved formulas are put into the same ranges in the sheets except for sheetsToExclude.
Modified script 2:
In this script, "Method: spreadsheets.values.batchGet" and "Method: spreadsheets.values.batchUpdate" are used.
function myFunction() {
// These variables are from your showing script.
var templateSheetName = "Template";
var sheetsToExclude = [templateSheetName, 'Sheet1', 'Sheet2'];
var ranges = ['C12:C35', 'C5:C6', 'G16:G19', 'G4'];
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ssId = ss.getId();
var sheets = ss.getSheets().filter(s => !sheetsToExclude.includes(s.getSheetName()));
var formulas = Sheets.Spreadsheets.Values.batchGet(ssId, { ranges: ranges.map(r => `'${templateSheetName}'!${r}`), valueRenderOption: "FORMULA" }).valueRanges;
var data = sheets.flatMap(s => formulas.slice().map(({ range, values }) => ({ range: range.replace(templateSheetName, s.getSheetName()), values })));
Sheets.Spreadsheets.Values.batchUpdate({ data, valueInputOption: "USER_ENTERED" }, ssId);
}
When this script is run, the formulas are retrieved from 'C12:C35', 'C5:C6', 'G16:G19', 'G4' of templateSheetName sheet, and the retrieved formulas are put into the same ranges in the sheets except for sheetsToExclude.
Note:
If you cannot use Sheets API, how about the following modified script? In this case, only the Spreadsheet service (SpreadsheetApp) is used.
function myFunction2() {
// These variables are from your showing script.
var templateSheetName = "Template";
var sheetsToExclude = [templateSheetName, 'Sheet1', 'Sheet2'];
var ranges = ['C12:C35', 'C5:C6', 'G16:G19', 'G4'];
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ssId = ss.getId();
var sheets = ss.getSheets().filter(s => !sheetsToExclude.includes(s.getSheetName()));
var formulas = ranges.map(r => ss.getSheetByName(templateSheetName).getRange(r).getFormulas());
sheets.forEach(s => ranges.forEach((r, i) => s.getRange(r).setFormulas(formulas[i])));
}
References:
Method: spreadsheets.batchUpdate
Method: spreadsheets.values.batchGet
Method: spreadsheets.values.batchUpdate

How to copy and paste as values Google Apps Script/Sheets from one spreadsheet to another?

So I have a huge spreadsheet with 9k rows and more than 30 columns. I want to copy this spreadsheet to another spreadsheet (Values only).
Previously I was using this code successfully, but due to the increase in data, the script now times out (1800s +). Is there a way to optimize this script or maybe an alternative option altogether?
function temp() {
var sss = SpreadsheetApp.openById('XYZ'); // sss = source spreadsheet
//var ss = sss.getSheets()[4]; // ss = source sheet
var ss = sss.getSheets(); // ss = source sheet
var id=4; //default number
for(var i in ss)
{
var sheet = ss[i];
if(sheet.getName()== "ABC")
{ id=i;
break;
}
}
console.log(id);
ss=sss.getSheets()[id];
//Get full range of data
var SRange = ss.getDataRange();
//get A1 notation identifying the range
var A1Range = SRange.getA1Notation();
//get the data values in range
var SData = SRange.getValues();
SpreadsheetApp.flush();
var tss = SpreadsheetApp.getActiveSpreadsheet(); // tss = target spreadsheet
var ts = tss.getSheetByName('ABC'); // ts = target sheet
//set the target range to the values of the source data
ts.getRange(A1Range).setValues(SData);
}
I believe your goal is as follows.
You want to reduce the process cost of the script.
In this case, I would like to propose to use Sheets API. This has already been mentioned in the Yuri Khristich's comment. Also, when the benchmark is measured between Spreadsheet service (SpreadsheetApp) and Sheets API, when Sheets API is used for reading and writing the values for Spreadsheet, it was confirmed that the process cost could be reduced. Ref
When Sheets API is used for your script, it becomes as follows.
Modified script:
Before you use this script, please enable Sheets API at Advanced Google services. And please set the source Spreadsheet ID and the sheet names.
function temp() {
var sourceSpreadsheetId = "XYZ"; // Please set the source Spreadsheet ID.
var destinationSpreadsheetId = SpreadsheetApp.getActiveSpreadsheet().getId();
var sourceValues = Sheets.Spreadsheets.Values.get(sourceSpreadsheetId, "ABC").values;
Sheets.Spreadsheets.Values.update({values: sourceValues}, destinationSpreadsheetId, "ABC", {valueInputOption: "USER_ENTERED"});
}
References:
Benchmark: Reading and Writing Spreadsheet using Google Apps Script
Method: spreadsheets.values.get
Method: spreadsheets.values.update
Copy from one spreadsheet to another
function copyfromonetoanother() {
const sss = SpreadsheetApp.getActive();
const dss = SpreadsheetApp.openById("dssid");
const ssh = sss.getSheetByName('Sheet1');
const vs = ssh.getDataRange().getValues();
const dsh = dss.getSheetByName('Sheet1');
dsh.getRange(dsh.getLastRow() + 1,1,vs.length,vs[0].length).setValues(vs);
}
If you wish to select the source range:
function copyfromonetoanother() {
const sss = SpreadsheetApp.getActive();
const dss = SpreadsheetApp.openById("dssid");
const ssh = sss.getSheetByName('Sheet1');
const vs = ssh.activeRange().getValues();
const dsh = dss.getSheetByName('Sheet1');
dsh.getRange(dsh.getLastRow() + 1,1,vs.length,vs[0].length).setValues(vs);
}
This function appends to the bottom of the destination sheet
function appenddatatobottomofdestination() {
const sssId = "source spreadsheet id";
const sss = SpreadsheetApp.openById(sssId);
const dss = SpreadsheetApp.getActive();
const dssId = dss.getId();
const ssh = sss.getSheetByName('Sheet1');//Source sheet
const srg = ssh.getRange(1,1,ssh.getLastRow(),ssh.getLastColumn());
const dsh = dss.getSheetByName("Sheet1");//Destination sheet
var vs = Sheets.Spreadsheets.Values.get(sssId, `${ssh.getName()}!${srg.getA1Notation()}`).values;
const drg = dsh.getRange(dsh.getLastRow() + 1, 1, vs.length,vs[0].length);//appends to bottom of spreadsheet
Sheets.Spreadsheets.Values.update({values: vs}, dssId, `${dsh.getName()}!${drg.getA1Notation()}`, {valueInputOption: "USER_ENTERED"});
}

How do I run Google Sheets App Script on all sheets (tabs) in a Google Sheet?

I am trying to make the following script run on all the sheets(tabs) EXCEPT for one in my Google Sheet. I need some help. Thanks.
function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
var cell = sheet.getRange("B4");
var refresh = parseInt(cell.getValue().toString());
var increment = refresh + 1;
cell.setValue(increment);
}
Assuming that your script gives you what you expect
All sheets :
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
ss.getSheets().forEach(function (sheet){
var cell = sheet.getRange("B4");
var refresh = parseInt(cell.getValue().toString());
var increment = refresh + 1;
cell.setValue(increment);
})
}
All sheets except a list (Edit by Cooper)
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
const exclA = ['Sheet1','Sheet2'];//excluded sheets
ss.getSheets().filter(sh => {!~exclA.indexOf(sh.getName())}).forEach(function (sheet){
var cell = sheet.getRange("B4");
var refresh = parseInt(cell.getValue().toString());
var increment = refresh + 1;
cell.setValue(increment);
})
}
bitwise not

how to copy from one google sheet to another with button

i have made this in excel but cannot use it online. so i made it in google sheets but it does not work. What i want to do:
data ( values only, no formats) from sheet Tmrw range C3:Y20 copy to sheet Today range C3:y20, and after that clear data from sheet Tmrw range C3:Y20
i used this code:
function moveValuesOnly () {
var ss = SpreadsheetApp.getActiveSpreadsheet ();
var source = ss.getRange ("Tmrw"!C3:Y20");
var destSheet = ss.getSheetByName("Today");
var destRange = destSheet.getRange("Today!C3:Y20");
source.copyTo (destRange,"Today!C3:Y20);
source.clear ();
}
it does not work. i am not surprised.
the file is here:
https://docs.google.com/spreadsheets/d/11uyaRe7khP9PywXKkEh5e5xWLvm68Nw58R6dHkaTxvk/edit?usp=sharing
Modification points:
When getRange of Class Spreadsheet, you can include the sheet name to the A1Notation.
In this case, ss.getRange("'Today'!C3:Y20") can be used instead of ss.getSheetByName("Today");.
"Tmrw"!C3:Y20" and "Today!C3:Y20 are not enclosed by ". In this case, please modify to "Tmrw!C3:Y20" and "'Tmrw'!C3:Y20", and "Today!C3:Y20" and "'Today'!C3:Y20".
In your situation, I thought that the arguments of copyTo of Class Range is copyTo(destination).
When above points are reflected to your script, it becomes as follows.
Modified script:
function moveValuesOnly() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var source = ss.getRange("'Tmrw'!C3:Y20");
var destRange = ss.getRange("'Today'!C3:Y20");
source.copyTo(destRange);
source.clear();
}
Note:
When you want to use getSheetByName, you can also modify your script as follows.
function moveValuesOnly() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var srcSheet = ss.getSheetByName("Tmrw");
var source = srcSheet.getRange("C3:Y20");
var dstSheet = ss.getSheetByName("Today");
var destRange = dstSheet.getRange("C3:Y20");
source.copyTo(destRange);
source.clear();
}
References:
A1 notation
getRange(a1Notation) of Class Spreadsheet
copyTo(destination) of Class Range
getSheetByName(name)

Run script in specified sheet in Google Sheets

I have made a speadsheet with Google Sheet which contains two sheets:
Name of sheet 1: Start
Name of sheet 2: Playlist
I have made the following script:
function shuffleSheet() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("A4:C15");
// Randomizes the range
range.randomize();
}
The script worked fine when I only had 1 sheet. Now I have two and I want the script to run on the sheet named Playlist.
I can't figure out how to do this. Please help.
Thanks.
If you only want to run the code on the "Playlist" sheet this should work for you.
function shuffleSheet() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Playlist");
var range = sheet.getRange("A4:C15");
// Randomizes the range
range.randomize();
}
I you want to run it on all sheets then you need to use a for loop.
function shuffleSheet() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
for (var i = 0; i < sheets.length; i++) {
var sheet = sheets[i];
var range = sheet.getRange("A4:C15");
// Randomizes the range
range.randomize();
}
}