CopyAfter() is meant to return the newly created sheet, but doesn't seem to - spreadsheetgear

Update: it turns out it was the SaveToStream() method which saves all sheets instead of the the one, that was the confusing part which lead me to believe the sheet being returned wasn't right
Given the documentation on the CopyAfter() method, the return value is the newly copied sheet
So I wrote the first line of code, now commented out (see below).
The problem is, when I use the sheetWithValues variable from the return of the method, the code below it, does not work (ie The formulas are not replaced with values)
When I comment out the use of the returned value from CopyAfter() and substitute it with workbook.Worksheets[0] as done in the code below, it does work (ie the formulas in the sheet are replaced with values)
Given that experiment, it doesn't seem possible that the return value of CopyAfter() is actually returning the newly created sheet, it's returning something else.
So why does the return value of CopyAfter() not return what is obviously expected here?
I really want to use the return value, because the alternative code below seems brittle and is unlikely to continue working if sheets are re-arranged.
The goal of the code BTW is simply to copy the entire sheet, with values, before exporting... shortly after this code, I take the copied sheet and stream it to PDF.
//var sheetWithValues = workbook.ActiveWorksheet.CopyAfter(workbook.ActiveWorksheet);
workbook.ActiveWorksheet.CopyAfter(workbook.ActiveWorksheet);
var sheetWithValues = workbook.Worksheets[0]; // no idea why this works and the return value does not
sheetWithValues.UsedRange.Value = sheetWithValues.UsedRange.Value

Based on your follow-up comments, it sounds like the problem is with the expectation of IWorksheet.SaveToStream(...), or any of the other IWorksheet.Save*() methods.
These methods will only ever save just the specified IWorksheet when you are saving to a text-based data file format like FileFormat.CSV or UnicodeText (tab-delimited / *.txt). Saving to an Excel file format like FileFormat.OpenXMLWorkbook will result in the entire workbook (so all sheets) being saved, as there simply are too many potential interdependencies on other parts of a workbook to extract and save only one sheet (even if in your case you expect to only have simple values, and not any formulas referring to other sheets, defined names, etc.)
Right now, it looks like you are making a copy of your sheet in the same workbook as the source. In this case you should delete the source sheet prior to saving, which will leave the copied sheet. Do note that if this workbook has any other sheets, those will be persisted as well. You can delete a sheet via the ISheet.Delete() method.
If your source workbook does have a bunch of other sheets that you don't want persisted in your destination workbook, an alternative to copying in the same workbook would be to create a new workbook (Spreadsheet.Factory.GetWorkbook() method) and copy the source sheet into it, after or before the default "Sheet1" worksheet. You'd still need to delete the default "Sheet1" via the ISheet.Delete() method so that only your copied sheet remains.

Related

How to move a checkbox with a row in google sheets with data sorted using SORT?

I am currently a college student who was trying to make a dynamic google sheet where my data is organized. The premise is as follows:
I input raw data into one spread sheet titled "Raw Data"
That data from raw data is automatically sorted by date using the SORT function to keep it dynamically updated.
If an item in the organized data is checked, then the row turns gray and updates into a corresponding chart.
My problem is that as I add or edit data, the checkbox is stuck to the row. In the first picture, you see the normal data as is.
organized spreadsheet data using a SORT function
However, lets say I add a due date for an assignment on 1/31/23. Then this happens.
Spreadsheet data following SORT function modified to show problem
So overall, my question is how can I lock a checkbox to a specific row so if the row is moved, the checkbox moves with it? If it is needed, I have a public link to the spreadsheet if it helps understand the problem:
Public Spreadsheet 1/31 for Clarification
I currently tried several data validation rules but most of them simply locked the cell from being edited if it was checked.
I also tried modifying the RAW data and adding a checkbox there before the sort function refreshes but that negates the point of the sheet at all then.
I was not sure how else to go about it trying to lock it into that place. Also, I was not sure how to upload as a MARKDOWN text table so I figured an image would suffice for the problem.

Align imported Data with manually added data

Example File: https://docs.google.com/spreadsheets/d/1Ht_2QMGMbHmlxSPoOiLX2vw78IL1wp3VhpOOc66eMqY/edit#gid=0
We are filling Point 1 - 4 manually. The data in A,B,C is sorted through C and will change every now and then. The problem I am noticing now is that A,B,C is moving. But D:G will stay in the same column.
We want to use this file to fill in the data since its our main file. So using the initial =query to also take into account D:G is not an option.
Would there be any other way to "link" D:G to the corresponding values in A:C?
Looking at your sheet I noticed you try a VLOOKUP formula.
Please try the following formula
=INDEX(IFERROR(VLOOKUP(A1:A;Blad1!A2:I;{6\7\8\9};0)))
Of course your approach would cause problems. You're trying to map manual data to some data that is bound to change. You can't expect the manual data to move or change in sync when the imported data changes.
You could probably make it work at least if the imported data does not change in order, and instead gets any new data appended. Even then, it doesn't help you if any of the imported rows gets deleted.
There are only two ways I could see to make this work:
Map your manual data as part of the original sheet where your other data is imported from. In other words, make D:G part of the source of A:C, if possible. This is the best approach. Works even when some imported rows get deleted or changed.
Don't sort A:C at source. Simply append new rows, and import as is. Keep your Blad1 sheet as the local source sheet, and add your manual data to D:G here. Then create a new sheet for sorting or do any other thing you'd like, and use this new sheet to feed your Blad2 sheet. This doesn't work if some of the imported rows get deleted or changed.

Google Sheets Filter Function - How can I make data static?

Im trying to make the data that I sort using a filter function stay static in the cells that is is sorted in. I have had this problem a few times and have tried looking it up on Youtube but it seems to be more of an advanced question.
For example when I filter a row of data from one sheet to the next. It is not static data, meaning when I delete the data on the new sheet it stays there until I delete the data on the master sheet.
Please few my example spreadsheet here.
Google Sheets Example Spreadsheet
Any formula (like FILTER) can only process source data. It is linked to the source data permanently. Think of it as a mirror of the source data (and think of changes the formula makes to the source data as a funhouse mirror). You cannot have formula results that you change manually.
If you want a copy of the original data that you can change (and that will no longer be linked to the original data at all), then just select the range containing the original data, Copy it and Paste it to the new location.
If you need some kind of link or updating when the source data changes, or you want the ability to change the source data when you update the copy you made, this requires writing custom scripts. It cannot be done with formulas.

How can I populate the "range" section of a QUERY function with the value from another cell's formula in Google Sheets?

Basically the title says it pretty well. I have a query I want to run that will continue to stack results of an ongoing event, each new event having the creation of a new sheet within this on Google Sheet.
The helper columns essentially act to concatenate the separate parts of the sheet names with the appropriate referenced cells.
My problem comes in when I try to use query and reference the populated cell as the range. I don't want Query to Query the CELL I am referencing, I want it to Query the TEXT that that cell contains. Does that make sense?
When I copy and paste the VALUE from O4, and then copy that value and paste it into the QUERY formula, it works as intended.
There is a script function "getSheetnames()" in cell E2, not sure if you need to know that to inspect the sheet.
I have gone around and around and tried every version of googling that I can think of, but all I keep finding is how to dynamically change the "query" part of the query, not the "data" part.
Here's the sheet (the tab in question is HELPER):
https://docs.google.com/spreadsheets/d/1n-fwDkeS5gtkXhPZZNszZs2_AlBeMN_2-5zo3sbUGho/edit?usp=sharing

How to automatically copy and paste cells Google Sheets

My goal is to write information in one column, and have it duplicated to another column. Lets say I have my data in cell =A3. I would like this data to automatically be copied into cell =Z50. The only way I know to do this without manually and writing the data or copy and pasting the cells is to go to cell =Z50 and in the function bar set it equal to =A3. I was hoping someone might know of a way to automate this process. By setting the column Z equal to the A column with the same row.
I was looking for the same thing, and even though I haven't tried the ArrayFormula solution, this could be another way to get this automatically copying done:
=QUERY(MySheet!B1:B100,"SELECT *")
Use this formula where you have planned to get the information copied.
"MySheet!B1:B100" = This will be the sheet you want to get data copied from.
"SELECT *" = This will get everything in this interval copied, in case you want any specific data into the interval, you have the option of choosing as well.
I found this tip at: https://productforums.google.com/forum/#!topic/docs/31xTcOHNRJM
This formula might work in cell Z50 (or anywhere else on the sheet that you want the data copied to):
=ArrayFormula(IF(A3:A="",,A3:A))

Resources