Sheet.hide() sometimes works sometimes not - google-apps-script

this is my final script
function archive2() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var source = ss.getSheetByName('Lista'); // name of source sheet
var sourceRange = source.getRange('A1:B'); // range to copy
var destination1 = ss.getSheetByName('Sheet2'); // name of log sheet
var lastRow = destination1.getLastRow();
sourceRange.copyTo(destination1.getRange(5000 + 1, 1),{contentsOnly:true});
var source = ss.getSheetByName('Sheet2'); // name of source sheet
var sourceRange = source.getRange('H1:I5000'); // range to copy
var destination2 = ss.getSheetByName('Sheet3'); // name of log sheet
var lastRow = destination2.getLastRow();
destination2.getRange('A1:B').activate();
destination2.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true});
var source = ss.getSheetByName('Sheet2'); // name of source sheet
var sourceRange = source.getRange('H1:I5000'); // range to copy
var destination3 = ss.getSheetByName('Sheet3'); // name of log sheet
sourceRange.copyTo(destination3.getRange(1 + 1, 1),{contentsOnly:true});
var spreadsheet = SpreadsheetApp.getActive();
var source = ss.getSheetByName('Sheet3'); // name of source sheet
var sourceRange = source.getRange('A1:B5000'); // range to copy
var destination4 = ss.getSheetByName('Sheet2'); // name of log sheet
var lastRow = destination4.getLastRow();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Sheet2'), true);
spreadsheet.getRange('A:B').activate();
spreadsheet.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true});
sourceRange.copyTo(destination4.getRange('A:B'),{contentsOnly:true});
var hide1 = ss.getSheetByName('Sheet2');
hide1.hideSheet();
var hide2 = ss.getSheetByName('Sheet3');
hide2.hideSheet();
}
Basically it makes a static copy of some data that I need to sum later. This part works fine.
What isn't working are the last lines, basically I want to hide Sheet2 and Sheet3, which are helper sheets to make the calcs.
The best way would be to make all the calcs without unhide those sheets but I wasn't able to find a solution (so if someone know what I've to change to perform it, would be great), so I'm searching a way to at least hide them after the script run.
With these lines, sometimes they are both hide, sometime just 1 of them (randomly between sheet2 and sheet3, I guess it have something to do with "active", and so I've tried to active both in sequence but still doesn't work).
Could you help me to hide both everytime (or make it works without unhide anything at all) without failures? Thanks
https://docs.google.com/spreadsheets/d/1GREsQNrDAaLqOROwAixajK6tpTRGOhqGHo0kfyyyuGw/edit#gid=0

Related

Quickest method of copy-pasting range of cells in Google App Script

Background
I wrote script for copying range of cells from one document to another. It works correctly. But depending on volume of data it could run for 10+ minutes. Which is odd, because manual copy-pasting of this data without script will took 2 minutes. I ran some tests that revealed most time-consuming piece of code that needs to be speed up
Question
What is the quickest method of copying range of cells from one Google Spreadsheet document to another? Or how should I change my code to speed it up?
// Copying 1st batch of data
var srcLastRow = srcSheet1.getLastRow();
var srcLastCol = srcSheet1.getLastColumn();
var srcRange = srcSheet1.getRange(2, 1, srcLastRow-1, srcLastCol);
var transfValues = srcRange.getValues();
destSheet.getRange(destLastRow + 1,1,srcLastRow-1, srcLastCol).setValues(transfValues);
Appendix
And here is full script, that runs correctly but slow, just in case
function sborka() {
// Destination sheet variables
var destSpreadSheet = SpreadsheetApp.getActiveSpreadsheet();
var destSheet = destSpreadSheet.getSheetByName("destination");
// Source sheet variables
var srcSpreadSheet1 = SpreadsheetApp.openById('xxxxxx');
var srcSheet1 = srcSpreadSheet1.getSheetByName("source 1");
var srcSpreadSheet2 = SpreadsheetApp.openById('ууууууу');
var srcSheet2 = srcSpreadSheet2.getSheetByName("source 2");
// Clear all except 1st row with headers
var destLastRow = destSheet.getLastRow();
destSheet.deleteRows(2,destLastRow)
// What is last row in destination sheet?
var destLastRow = destSheet.getLastRow();
// Copying 1st batch of data
var srcLastRow = srcSheet1.getLastRow();
var srcLastCol = srcSheet1.getLastColumn();
var srcRange = srcSheet1.getRange(2, 1, srcLastRow-1, srcLastCol);
var transfValues = srcRange.getValues();
destSheet.getRange(destLastRow + 1,1,srcLastRow-1, srcLastCol).setValues(transfValues);
// What is last row in destination sheet now?
var destLastRow = destSheet.getLastRow();
// Copying 2nd batch of data
var srcLastRow = srcSheet2.getLastRow();
var srcLastCol = srcSheet2.getLastColumn();
var srcRange = srcSheet2.getRange(2, 1, srcLastRow-1, srcLastCol);
var transfValues = srcRange.getValues();
destSheet.getRange(destLastRow + 1,1,srcLastRow-1, srcLastCol).setValues(transfValues);
// Done
}
Try this:
var srcSheet1 = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var destSS = SpreadsheetApp.openById("destination ss id");
//Copy Source Sheet
srcSheet1.copyTo(destSS);
var newSrcSheet1 = destSS.getSheetByName("Copy of "+srcSheet1.getName());
Logger.log(newSrcSheet1);
//Copy range
var destSheet = destSS.getSheetByName("Sheet1");
var srcLastRow = newSrcSheet1.getLastRow();
var srcLastCol = newSrcSheet1.getLastColumn();
var srcRange = newSrcSheet1.getRange(2, 1, srcLastRow-1, srcLastCol);
srcRange.copyTo(destSheet.getRange(destSheet.getLastRow() + 1,1));
//Delete Sheet
destSS.deleteSheet(newSrcSheet1);
I copied first the source sheet to the destination spreadsheet. The name of the copied sheet will be "Copy of [original sheet name]". Refer to Sheet.copyTo()
Then, I copied the range from the new source sheet to the destination sheet using Range.copyTo(destination)
Lastly, I deleted the new source sheet.

Why is this copying to the new sheet at varying intervals?

I'm copying data from one sheet to another 3x per week. The triggers are working fine but the data is supposed to copy to a new row sequentially. However, right now it's copying with varying numbers of rows in between them and I cant figure out why.
I have tried deleting all of the data in the sheet, deleting the sheet and recreating it, and double checked how I've input what data needs to be copied.
var logSheet = ss.getSheetByName('Sheet1');
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName('Data Sheet');
var RDSheet = ss.getSheetByName('RawDataSheet');
var lastRow = logSheet.getLastRow();
var RawData = sourceSheet.getRange(45,1,1,34);
RawData.copyTo(RDSheet.getRange(lastRow + 1, 1), {contentOnly: false});
I expect the range (45,1,1,34), one long row, to copy itself to the RawDataSheet and when it triggers it places the data 1 row below the previously copied data.
Try this:
function myfunction() {
var ss=SpreadsheetApp.getActive();
var logSheet=ss.getSheetByName('Sheet1');
var sourceSheet=ss.getSheetByName('Data Sheet');
var RDSheet=ss.getSheetByName('RawDataSheet');
var lastRow=logSheet.getLastRow();
var RawData=sourceSheet.getRange(45,1,1,34);
RawData.copyTo(logSheet.getRange(lastRow + 1, 1), {contentOnly: false});
}

Append rows from other tabs without losing formatting

In an effort to organize a set of bank account and credit card statements, I download them. Then I open them in Excel or Google Sheets, reorganize them in matching columns, put all in one tab (TOTAL), and then sort.
The question is now how I do the copy from the individual sheets. I think I'm pretty close after pasting and repeating (the manual way) some function I found by searching. The problem is that this function removes all formatting from the source rows
Example sheet
The last tab example_TOTAL is put there only to show how I want tab TOTAL to look after the process is done.
Here's my function so far which does the most, but erases formatting (colors).
function Copy() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var ss = spreadsheet.getSheetByName('TabA'); //replace with source Sheet tab name
var range = ss.getRange('A1:L'); //assign the range you want to copy
var data = range.getValues();
var ts = spreadsheet.getSheetByName('TOTAL'); //replace with destination Sheet tab name
ts.getRange(ts.getLastRow()+1, 1, data.length, data[0].length).setValues(data);
var ss = spreadsheet.getSheetByName('TabB'); //replace with source Sheet tab name
var range = ss.getRange('A1:L'); //assign the range you want to copy
var data = range.getValues();
ts.getRange(ts.getLastRow()+1, 1, data.length, data[0].length).setValues(data);
var ss = spreadsheet.getSheetByName('TabC'); //replace with source Sheet tab name
var range = ss.getRange('A1:L'); //assign the range you want to copy
var data = range.getValues();
ts.getRange(ts.getLastRow()+1, 1, data.length, data[0].length).setValues(data);
var ss = spreadsheet.getSheetByName('TabD'); //replace with source Sheet tab name
var range = ss.getRange('A1:L'); //assign the range you want to copy
var data = range.getValues();
ts.getRange(ts.getLastRow()+1, 1, data.length, data[0].length).setValues(data);
}
function copyFromTo() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var ss = spreadsheet.getSheetByName('TabA');
var copy = ss.getRange('A1:L');
var target = spreadsheet.getSheetByName('TOTAL');
var dest = target.getRange('A1:L');
copy.copyTo(dest);
var ss = spreadsheet.getSheetByName('TabB');
var copy = ss.getRange('A1:L');
copy.copyTo(dest);
var ss = spreadsheet.getSheetByName('TabC');
var copy = ss.getRange('A1:L');
copy.copyTo(dest);
var ss = spreadsheet.getSheetByName('TabD');
var copy = ss.getRange('A1:L');
copy.copyTo(dest);
}
How can I append from these sheets and still keep the formatting?

copyTo(destination, copyPasteType, transposed) stops rest of function from executing

I'm trying to use the function copyTo(destination, copyPasteType, transposed) to copy a sheet in one file to another sheet in a different file while keeping everything, including column width, the same. Using just the function copyTo(destination), my code works and I get everything I want in a different file, EXCEPT the column width. However, when I try and use copyTo with the copyPasteType argument, the sheet is copied perfectly (with the correct column widths), BUT any subsequent code isn't run. Why is this happening, and how can I fix it?
function createSpreadsheet(foldername) {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName('Sheet1');
var folder = DriveApp.getFolderById(foldername);
RESPONSE = FileName(); //a function that prompts the user for a filename
//creates a new spreadsheet which has two sheets, and deletes the "Copy of Sheet1" sheet
var newSpreadsheet = SpreadsheetApp.create(RESPONSE);
var newsheet = newSpreadsheet.getSheetByName('Sheet1');
var copysheet = sheet.copyTo(newSpreadsheet);
sheet.copyTo(newSpreadsheet, SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
//when using CopyPasteType argument, everything from here down isn't run
copysheet.getDataRange().copyTo(newsheet.getDataRange());
copysheet.getDataRange().copyTo(newsheet.getDataRange(),
SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
newSpreadsheet.deleteSheet(copysheet);
var newSpreadsheetID = newSpreadsheet.getId();
var file = DriveApp.getFileById(newSpreadsheetID);
folder.addFile(file);
DriveApp.removeFile(file);
clearYellow(); //clears contents of yellow cells
clearGray(); //clears contents of gray cells
}
I've also asked this question in Google+
I didn't find an answer as to why the copyTo with three arguments stopped the rest of my function from executing, but I found another way to copy the column widths over to the new spreadsheet that works.
function createSpreadsheet(foldername) {
var oldspreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var oldsheet = oldspreadsheet.getSheetByName('Sheet1');
var folder = DriveApp.getFolderById(foldername);
RESPONSE = FileName();
//creates a new spreadsheet which has two sheets, and deletes the "Copy of Sheet1" sheet
var newSpreadsheet = SpreadsheetApp.create(RESPONSE);
var newsheet = newSpreadsheet.getSheetByName('Sheet1');
var copysheet = oldsheet.copyTo(newSpreadsheet);
copysheet.getDataRange().copyTo(newsheet.getDataRange());
var numberOfColumns = oldsheet.getLastColumn();
for (columnNumber = 1; columnNumber <= numberOfColumns; columnNumber++){
var Width = oldsheet.getColumnWidth(columnNumber);
newsheet.setColumnWidth(columnNumber, Width);
};
newSpreadsheet.deleteSheet(copysheet);
var newSpreadsheetID = newSpreadsheet.getId();
var file = DriveApp.getFileById(newSpreadsheetID);
folder.addFile(file);
DriveApp.removeFile(file);
clearYellow(); //clears contents of yellow cells
clearGray(); //clears contents of gray cells
}

Script for a button in google sheets

I created a button that does the following:
A. Copy a group of columns from sheet 1 (daily sports) and paste them into sheet 2 (Record).
B. The columns pasted in sheet 2 go to the next empty column (or create a new column) so they don't over-right the info that's already there..
C. Every time I'm done putting in new information I press the button and it automatically gets copied to sheet 2.
This is the code I'm using:
function moveValuesOnly () {
var ss = SpreadsheetApp.getActiveSpreadsheet ();
var source = ss.getRange ("Daily sports!B2:D20");
var destSheet = ss.getSheetByName("Record");
var destRange = destSheet.getRange(destSheet.getLastRow(),1);
var destRange = destSheet.getRange(destSheet.getLastColumn(),2);
source.copyTo (destRange, {contentsOnly: true});
source.clear ();}
The problems I'm having are as follows
1) Every time I hit the button it does not create a new column, instead it just covers the old columns.
2) the original source gets cleared (I know, It's the last function, it clearly says "clear" I just don't know what to write instead... I want the source to stay as is)
If anyone could help me with this script that would be great..
Thank you!
The following code should do what are you looking for:
function moveValuesOnly() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var source = ss.getRange ("Daily sports!B2:D20");
var destSheet = ss.getSheetByName("Record");
var row = 1; //the starting row of the range
var column = destSheet.getLastColumn()+1; //the starting column of the range
var numRows = 19; //the number of rows to return
var numColumns = 3; //the number of columns to return
var destRange = destSheet.getRange(row, column, numRows, numColumns);
source.copyTo(destRange, {contentsOnly: true})
}