I have a case that i want to delete blank rows for multiple sheets. I want to use Google Sheet API because the SpreadsheetApp method takes too long and i get the timeout error because of it. I have 10k+ rows.
I already made my own code (Spreadsheet method version):
function myFunction() {
const sheet = ['', '', '', '']; //sheets name
for (let i in sheet) {
const ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheet[i]);
const maxrow = sheet.getMaxRows();
const lastrow = sheet.getLastRow();
if (maxrow - lastrow > 10) {
ss.deleteRows(lastrow + 10, maxrow - lastrow - 10);
}
}
}
I believe your goal is as follows.
You want to modify your showing script using Sheets API.
In your script, it seems that sheet is an array. So, I think that an error occurs at const maxrow = sheet.getMaxRows();. Please be careful about this.
In this case, how about the following modification?
Modified script:
Please enable Sheets API at Advanced Google services. Ref
function myFunction() {
const sheet = ['Sheet1', 'Sheet2',,,]; // Please set sheet names you want to use.
const ss = SpreadsheetApp.getActiveSpreadsheet();
const requests = sheet.reduce((ar, s) => {
const sheet = ss.getSheetByName(s);
const maxrow = sheet.getMaxRows();
const lastrow = sheet.getLastRow();
if (maxrow - lastrow > 10) {
ar.push({ deleteDimension: { range: { sheetId: sheet.getSheetId(), startIndex: lastrow + 10, endIndex: maxrow, dimension: "ROWS" } } });
}
return ar;
}, []);
if (requests.length == 0) return;
Sheets.Spreadsheets.batchUpdate({ requests }, ss.getId());
}
References:
Method: spreadsheets.batchUpdate
DeleteDimensionRequest
Related
Is there a app script I can run that will update the ranges of all the filter views on a sheet at once?
I have hundreds of filter views, and it would be laborious to do it manually.
The filter views are all on a sheet called "Data'. I need to change the range from A1:AB3116 TO A1:AB9011
Thanks for any help.
I believe your goal is as follows.
You want to change the range of filter views.
You want to change from "A1:AB3116" to "A1:AB9011" of all filter views in "data" sheet.
In this case, how about the following sample script?
Sample script:
Please copy and paste the following script to the script editor of Spreadsheet and enable Sheets API at Advanced Google services, and save the script.
Please confirm sheetName and obj. In this sample, your provided information is used.
function myFunction() {
var sheetName = "data"; // This is from your question.
var obj = [{ before: "A1:AB3116", after: "A1:AB9011" }]; // This is from your question.
// Retrieve spreadsheet and sheet.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var spreadsheetId = ss.getId();
var sheet = ss.getSheetByName(sheetName);
var sheetId = sheet.getSheetId();
// Convert a1Notation to gridRange.
var o = obj.map(({ before, after }) =>
[before, after].map(r => {
var rng = sheet.getRange(r);
var rowStart = rng.getRow() - 1;
var rowEnd = rowStart + rng.getNumRows();
var colStart = rng.getColumn() - 1;
var colEnd = colStart + rng.getNumColumns();
return { sheetId, startRowIndex: rowStart, endRowIndex: rowEnd, startColumnIndex: colStart, endColumnIndex: colEnd };
})
);
// Create request body for using the batchUpdate of Sheets API.
var filterViews = Sheets.Spreadsheets.get(spreadsheetId, { ranges: [sheetName], fields: "sheets(filterViews)" }).sheets[0].filterViews;
var requests = filterViews.reduce((ar, { range, ...e }) => {
var check = o.find(([{ startRowIndex, endRowIndex, startColumnIndex, endColumnIndex }]) => range.startRowIndex == startRowIndex && range.endRowIndex == endRowIndex && range.startColumnIndex == startColumnIndex && range.endColumnIndex == endColumnIndex);
if (check) {
ar.push({ updateFilterView: { filter: { filterViewId: e.filterViewId, range: check[1] }, fields: "*" } });
}
return ar;
}, []);
// Reuest Sheets API using the created request body.
if (requests.length == 0) return;
Sheets.Spreadsheets.batchUpdate({ requests }, spreadsheetId);
}
When this script is run, the filter views are retrieved from "data" sheet. And, the range of A1:AB3116 is searched from the retrieved filter views, and when it is found, the range is changed to A1:AB9011 and update the filter views.
In this sample, when you change multiple changes of ranges, you can use them in obj.
References:
Method: spreadsheets.get
Method: spreadsheets.batchUpdate
Related thread
Script to Update Multiple Google Sheet Filter View Ranges
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
I'm trying to build a Macro to erase all the rows that have empty values on column D. Originally, I was using this code that I found:
function deleteRows() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName('Datos Competidor 2 - EV');
var r = s.getRange('D:D');
var v = r.getValues();
for(var i=v.length-1;i>=0;i--)
if(v[0,i]=='')
s.deleteRow(i+1);
};
However the excessive number of calls to the API made this really slow and some times even fail due to a timeout.
I decided to just add all the rows that met the condition to a list and then just pass that to the deleteRow() in order to only call the API once using this code:
function deleteBlankRows() {
emptyRange=[]
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName('Datos Competidor 2 - EV');
var r = s.getRange('D:D');
var v = r.getValues();
for(var i=v.length-1;i>=0;i--)
if(v[0,i]=='')
emptyRange.push((i)+":"+(i));
ss.getRangeList(emptyRange).activate();
ss.getActiveSheet().deleteRows(ss.getActiveRange().getRow(), ss.getActiveRange().getNumRows());
};
The execution seems to work just fine, completing in 1 to 2 seconds, however, rows aren't erased as much as selected by the end of the execution.
This is what I see:
Final result
Any ideas why this is happening?
Thanks!
I believe your goal is as follows.
You want to reduce the process cost of your script.
In this case, how about using Sheets API? When Sheets API is used, I thought that the process cost for deleting the rows can be reduced a little. When the Sheets API is reflected in your script, it becomes as follows.
Modified script:
Before you use this script, please enable Sheets API at Advanced Google services.
function deleteRows() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName('Datos Competidor 2 - EV');
var values = s.getRange('D1:D' + s.getLastRow()).getDisplayValues();
var sheetId = s.getSheetId();
var requests = values.reduce((ar, [d], i) => {
if (!d) ar.push({ deleteDimension: { range: { sheetId, startIndex: i, endIndex: i + 1, dimension: "ROWS" } } });
return ar;
}, []).reverse();
Sheets.Spreadsheets.batchUpdate({ requests }, ss.getId());
}
References:
Method: spreadsheets.batchUpdate
DeleteDimensionRequest
Delete Rows with empties on column D
function deleteBlankRows() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const s = ss.getSheetByName('Datos Competidor 2 - EV');
const r = s.getRange('D1:D' + s.getLastRow());
const v = r.getValues().flat();
let d = 0;
v.forEach((e, i) => {
if (!e) {
s.deleteRow(i + 1 - d++)
}
})
}
I am trying to transfer rows of data from Sheet 1 to Sheet 2.
Thank you
I believe your goal is as follows.
Your script works fine. But, you want to reduce the process cost of your script.
In this case, how about the following modification?
Modified script:
In this modification, the rows are copied using setValues and deleted by Sheets API. By this, I thought that the process cost might be able to be reduced a little. So, please enable Sheets API at Advanced Google services before you use this script.
function archive() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var firstSheet = ss.getSheetByName("Sheet 1");
var firstSheetValues = firstSheet.getRange(1, 1, firstSheet.getLastRow(), firstSheet.getLastColumn()).getValues();
var sheetId = firstSheet.getSheetId();
var {values, requests} = firstSheetValues.reduce(function (o, r, i) {
if (r[1] == "Archive") {
o.values.push(r);
o.requests.push({ deleteDimension: { range: { sheetId, startIndex: i, endIndex: i + 1, dimension: "ROWS" } } });
}
return o;
}, { values: [], requests: [] });
var secondSheet = ss.getSheetByName("Sheet 2");
secondSheet.getRange(secondSheet.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);
Sheets.Spreadsheets.batchUpdate({requests: requests.reverse()}, ss.getId());
}
References:
Method: spreadsheets.batchUpdate
DeleteDimensionRequest
I have a sheet with more than 3000 rows and I want to group those rows by a parameter in ColA. So all rows having '1' in colA should be grouped under the row above with '0' in colA. Once groups are created I want them to collapse.
Since the script will be triggered daily upon data update, I also need the before-created groups to be removed so the new ones can be properly created.
I have several scrips doing what I need but it takes forever for them to go through all the rows. Is it possible to optimize them in some way or perhaps a different approach can be used for my needs? Thanks in advance for your help!
function removeAllGroups1() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Pipeline");
const rg = sh.getDataRange();
const vs = rg.getValues();
vs.forEach((r, i) => {
let d = sh.getRowGroupDepth(i + 1);
if (d >= 1) {
sh.getRowGroup(i + 1, d).remove()
}
});
}
function groupRows1() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Pipeline');
const levels = sh.getRange(2, 1, sh.getLastRow() - 1).getValues().flat();
levels.forEach((e, i) => sh.getRange(i + 2, 1).shiftRowGroupDepth(e));
}
function collapse() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Pipeline');
let lastRow = sh.getDataRange().getLastRow();
for (let row = 1; row < lastRow; row++) {
let depth = sh.getRowGroupDepth(row);
if (depth < 1) continue;
sh.getRowGroup(row, depth).collapse();
}
}
Data Sample: https://docs.google.com/spreadsheets/d/10BNrnAyQw89gy-Sj3CLiz4AgVtFI0AjXTvc0REGGTfY/edit#gid=113574154
I believe your goal is as follows.
You want to group the rows with high process speed.
You want to delete all groups with high process speed.
You want to collapse all groups with high process speed.
From the above goal, I thought that when Sheets API is used, the process cost can be reduced. And, Sheets API can group rows, delete all groups and collapse all groups.
Sample script:
Before you run this script, please enable Sheets API at Advanced Google services.
function removeAllGroups2() {
const sheetName = "sample";
const ss = SpreadsheetApp.getActive();
const ssId = ss.getId();
const sheetId = ss.getSheetByName(sheetName).getSheetId();
const n = Sheets.Spreadsheets.get(ssId, { ranges: [sheetName] }).sheets[0].rowGroups.reduce((n, { depth }) => n < depth ? depth : n, 0);
const requests = Array(n).fill("").map(_ => ({ deleteDimensionGroup: { range: { sheetId, dimension: "ROWS" } } }));
Sheets.Spreadsheets.batchUpdate({ requests }, ssId);
}
function groupRows2() {
const sheetName = "sample";
const ss = SpreadsheetApp.getActive();
const sheet = ss.getSheetByName(sheetName);
const levels = sheet.getRange("A2:A" + sheet.getLastRow()).getValues();
const sheetId = sheet.getSheetId();
const requests = levels.flatMap(([a], i) => Array(a).fill("").map(_ => ({ addDimensionGroup: { range: { sheetId, startIndex: i + 1, endIndex: i + 2, dimension: "ROWS" } } })));
Sheets.Spreadsheets.batchUpdate({ requests }, ss.getId());
}
function collapse2() {
const ss = SpreadsheetApp.getActive();
const requests = Sheets.Spreadsheets.get(spreadsheetId, {ranges: ["sample"]}).sheets[0].rowGroups.map(r => {
r.collapsed = true;
return { updateDimensionGroup: { fields: "*", dimensionGroup: r }};
});
Sheets.Spreadsheets.batchUpdate({ requests }, ss.getId());
}
Note:
I tested these sample scripts using your sample Spreadsheet. So when the structure of the Spreadsheet is different from your sample Spreadsheet, these scripts might not be able to be used. Please be careful about this. At first, please test them using your sample script.
References:
Method: spreadsheets.get
Method: spreadsheets.batchUpdate
DeleteDimensionGroupRequest
AddDimensionGroupRequest
UpdateDimensionGroupRequest