I have a code by which I remove all empty rows and columns from a sheet. Is it possible to use a batch request to speed up the script's work, because when there are a lot of files, it takes a very long time?
Code below
while ( files.hasNext()){
var file1 = files.next().getId();
var sheet = SpreadsheetApp.openById(file1);
var allsheets = sheet.getSheets();
for (var s in allsheets){
var sheet=allsheets[s]
var maxColumns = sheet.getMaxColumns();
var lastColumn = sheet.getLastColumn();
var maxRows = sheet.getMaxRows();
var lastRow = sheet.getLastRow();
if (maxRows-lastRow === 0){}
else {
if (maxRows-lastRow != 0){
sheet.deleteRows(lastRow+1, maxRows-lastRow);
}
console.log(sheet)
if (maxColumns-lastColumn != 0){
sheet.deleteColumns(lastColumn+1, maxColumns-lastColumn);
}
console.log(sheet)
}
}
}
I would be grateful for any help
I believe your goal as follows.
Modification points:
When above points are reflected to your script, it becomes as follows.
Modified script:
Before you use this script, please enable Sheets API at Advanced Google services.
Note:
I think that above sample script might be suitable when the number of files is small. But, when the number of files are large, when the file list is retrieved using Drive API, the process cost might be able to be reduced more. In this case, the process cost for retrieving the file list using Drive API v3 is lower than that using Drive API v2 of Advanced Google services. So this sample script used Drive API v3 using UrlFetchApp. In this sample script, from your script, it supposes that the files are existing in just under a folder. Please be careful this.
When you use this script, please enable Drive API at Advanced Google services.
References: