EPPlus Tabs become grouped using MovetoStart - epplus

I use EPPlus (current version) to create a new ExcelPackage using an existing template file that has several other tabs. I add a new tab, which gets added as the last tab, and populate the data. If I perform a MovetoStart function on the worksheet, it successfully moves the worksheet, but groups it with what was previously the first tab. While it's pretty easy to ungroup with a right-click, I'm trying to highly automate a process and a workaround is not what I need. Suggestions?

I just ran into this. I don't know why it occurs, but a workaround that worked for me is to select the tab after you reorder the sheets:
var worksheet = workbook.Worksheets.Add("Summary");
workbook.Worksheets.MoveToStart("Summary");
worksheet.View.SetTabSelected();

Related

Is there a way to copy an entire Spreadsheet with only values and no formulas/links (Google Sheets)?

The issue I am facing is trying to automate my weekly occurrence of coping an entire Spreadsheet to make a copy of it. Each week I need to hit "File --> Make a copy --> share with same people" and after doing so on the copy I need to hit "Allow Access" multiple times for each table that requires access, if I do not do this, there is no data displayed.
So I am wondering if there is a way to create a copy of a Spreadsheet where the copy contains entirely plain text and no formulas or links that way all the data can be read as soon as a copy is made.
This could be a separate question, but if anyone also knows how to automate hitting the "allow access" button for multiple tables in the copy that would also be helpful.
*To give an idea of the layout, essentially I have a main Spreadsheet (the one I make a copy of) that references data from other Spreadsheets (that are linked to google forms), and then I make a copy of the main Spreadsheet, and in making this copy is where I am required to hit the access button for each table.
Answer:
You can do this with Apps Script.
Code Example:
function duplicateSpreadsheet() {
const idOfSheetToCopy = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
var file = DriveApp.getFileById(ifOfSheetToCopy).next()
const newFile = file.makeCopy()
SpreadsheetApp.openById(newFile.getId()).getSheets().forEach(function(sheet) {
sheet.getDataRange().setValues(sheet.getDataRange().getDisplayValues())
})
}
Code Rundown:
Define Template Sheet ID
Make a copy of the sheet using DriveApp
Get the ID of the newly created sheet and open it with SpreadsheetApp
Loop through all sheets in the new copy and replace all cell values for the cell's display value
Instead of pasting with "Ctrl+V" use "Ctrl+Shift+V" for pasting without formatting. This will ignore things like links, font, and font size.
Not sure about the allow access button.

Snowflake: Worksheet1 was not saved because saving would overwrite new changes on the server

I received an prompt from snowflake reading
*Worksheet1 was not saved because saving would overwrite new changes on the server. Please decide what to do next:
It provides two options,
a) Overwrite Changes
b) Save to new worksheet.
Q: What changes to the server? is there a way to see that? Is this just regarding Worksheets?
Q: What is the safest option, New Worksheet?
I would go with new worksheet, however if you ever loose a worksheet there is a really cool feature that shows all historical worksheets associated with the account/role you are logged into here:
Click on the drop arrow next to the furthest to the right worksheet > Select "Open Worksheet"
All of the worksheets that were closed can be opened again.

Copy tabs/sheets from one workbook to another with macro/vba code (1 button)

two (hopefully) easy questions -
Can I copy 2 tabs from File1.xlsm to a master.XLSX file and still run the macro code? I assume not?
Assuming I cannot do #1, can I copy the 2 tabs into another master.xlsm file? If so, please help I've tried the following.
File1.xlsm just has a grid with a 1 button that calls 'File1.xlsm'!getData which populates the grid.
I was able to right click the tabs in File1.xlsm and move/copy them successfully into master.xlsm but without the vba getData Sub(). When I click the button it said it couldn't find the !getData code. Is there a clean way to copy the 2 tabs, and !getData macro code into the Master.xlsm?
Thanks a bunch in advance!
To accomplish this task, you should consider adding the relevant code into a module within your File1 workbook. You will need to enable the Visual Basic extensibility within your File1 workbook library.
You would then need to copy the module along with the two sheets into the new workbook. You would do this with commands similar to the following.
ActiveWorkbook.VBProject.VBComponents("module1").Export Path
SomeWorkbook.VBE.VBProject.VBComponents.Import Path
Where path is a string variable to a place where you would like to place the module, and someworkbook would be your Master.xlsm workbook object.
with ActiveWorkbook.VBProject
.VBComponents("sheet1").codemodule.addfromstring .VBComponents("Sheet1").codemodule.lines(1,.VBComponents("Module1").codemodule.CountofLines)
end with
sheets(array("sheet1","sheet2")).copy

Click to toggle sheet tab

I am trying to use VBA to create the same action you would do if you had an Excel spreadsheet open with 3 sheet and you want to toggle between sheets exactly like when you click on the sheet tab at the bottom of the workbook (i.e. Toggle between Sheet1, Sheet2, Sheet3 by clicking on the sheet tabs)
I've tried some thing:
book.Worksheets(1).Activate = True
book.Worksheets(1).Activate
sheet.Visible = True
But nothing seems to work. I'm not sure Activate is what I want. I think that just makes the sheet active, but not visible. Visible seems to do nothing. How can this be accomplished?
book.Worksheets(1).Activate
Is designed to perform the action you want "clicking on a tab"
You can also try
book.Worksheets(1).Select
Which can be used to select multiple tabs (see Have a look at What is the difference between Sheets.Select and Sheets.Activate?)
I am guessing something is not right either with your workbook reference or something else in your code. If you make a new excel file and just try Sheets("Sheet2").Activate it will select that tab.
thanks for the feedback. I have been attempting to use com objects in python. they usually interop with each other quite well, but this is one thing that doesn't seem to work. I've tried all of your suggestions with no results.
I managed to find a workaround by just moving the sheets around using:
sheet.Move(Before=book.Worksheets(1))
For some reason, when the sheet is moved, it also activates it which is my desired end result. It works for the scenario I am scripting, but may not for other people trying to do something similar.

How to copy conditionally formatted cells from Calc as a table into Writer

I have a LibreOffice Calc spreadsheet that uses some conditional formatting of cells. I would like to copy it into Writer as a table. The colours/formats of the cells should remain as they were due to the conditional formatting in Calc. Unfortunately when I do that, the formatting vanishes.
How can I copy it keeping the formatting?
Of course the Writer version no longer has to be conditional, but I need to keep current colours.
My work is done so eventually I can do the trick in Calc first (abandon the "conditional" part, and just preserve the formatting as - is). However due to amount of data I would prefer not to do it manually.
Is macro the only way to do that?
Use Insert -> Object -> OLE Object
Choose Create from file
Pick the right .ods file.
If you want to modify further (in my case - I need to create many tables from one spreadsheet as the original file is humongous - up to CL column) - do not tick "Link to the file" option.
After pressing OK, the spreadsheet is inserted as is (cloned and embedded), with the conditional formatting. Can be further modified (e.g. rows/cols can be deleted, hidden or whatever is needed). The conditional formatting remains active.
I personally prefer to copy as an image. This ensures the format is always exactly as it was in the spreadsheet and that no weird OLE/DDE links go wrong.
However, you specifically ask for a table. For that there are three (or 2.5) options:
Insert the entire as spreadsheet as an object. In Windows that can be done as Ister describes in his answer. This will be editable as an inline mini-sheet (Writer will invoke Calc for any editing actions).
Insert a part of the sheet as an object: Select what you want in the document, copy to the clipboard, go to Writer and select Edit->Paste Special. Then select the OLE option, or if on Linux, select "calc8". This will be editable as an inline mini-sheet.
Insert as HTML. This creates a standalone table. Formatting will not be 100% as in the sheet, as fonts, etc, will be reset by Writer, but it is a native Writer table that you can manipulate in Writer without invoking Calc. Colors, etc, are preserved.
If you use any of the object embedding options, you'll notice that formulae are kept intact (when not referring the data outside the pasted sheet or region). If you want all the data to be verbatim, then you need an intermediate step:
Select the data in your original sheet that you wish
Copy to the clipboard
Create a new sheet and place the cursor in the same spot as the first cell of the copied data (e.g. if your copied region is B4:X99, then place the cursor in B4 of the new sheet)
Select Edit->Paste Special
In the Paste Special window, check only the following options and click OK:
Text
Numbers
Date & Time
Formats