I have 2 requirements:
1) I want to create a new sheet from existing sheet in current spreadsheet programmatically
2) I want to copy one sheet from one spreadsheet to another spreadsheet programmatically
Help appreciated.
Since you have not specified the environment/language. I think the easiest way to do this is using Apps Script as it's built-in in Google Spreadsheets.
Here is a sample code that does this:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('SheetName');
sheet.copyTo(ss).setName('NewName'); //copy to the same spreadsheet.
//get a different spreadsheet (you can get its id in the url)
var os = SpreadsheetApp.openById('any-spreadsheet-key-that-you-can-edit');
//copy sourceSheet from one spreadsheet to another
sheet.copyTo(os).setName('AnotherName');
}
To run this, just open the spreadsheet with the sheet you want to copy, click the menu Tools > Script editor.. Paste the code, Save and Run > myFunction
Related
I have a document saved on SharePoint that documents testing.
Once testing has been completed I would like to move the tests (entire row) to a separate sheet to archive the test schedule, in the next empty row going down, and because its now archived delete it from the submission sheet.
Both sheets are within the same workbook. The number of rows differ each time the script is run therefore the script code would need to work based on a selection input by the user (either prior to running the script or prompted by the script).
I managed this in VBA. I can't locate a viable alternative to the selection function. Is there any way of translating the below VBA code to Office script so the same thing happens in Excel online?
Sub MoveCompletedTests()
Selection.Copy Sheets("Archive - ATL staff only").Range("A1048576").End(xlUp).Offset(1, 0)
Selection.Delete
End Sub
I have a button on the "Sample submission" sheet that runs the above code on the selected range on that sheet moving it to the "Archive - ATL staff only" sheet.
I attempted to use the script recorder but the script didn't allow for the selection to be dynamic enough, it coded for the cell range rather than just selection.
I think that something like getSelectedRange should work (at least, if you are not working with several selected ranges at once):
function main(workbook: ExcelScript.Workbook)
{
let destination: ExcelScript.Range;
let source = workbook.getSelectedRange().getEntireRow();
let archive = workbook.getWorksheet("Archive - ATL staff only");
if(archive.getUsedRange() == undefined) {
// if the archive sheet is empty, use the first row as a destination
destination = archive.getRange("1:1");
}
else {
// ... otherwise, use the next row after the last used
destination = archive.getUsedRange().getRowsBelow().getEntireRow();
}
destination.copyFrom(source, ExcelScript.RangeCopyType.values);
source.delete(ExcelScript.DeleteShiftDirection.up);
}
p.s. To be clear, in case of VBA a Selection is a property of Application. But in case of ExcelScript it's a method of a Workbook object. This can be a bit confusing because there is an ExcelScript.Application interface, which has no mention of Selection.
You can try the code here:
function main(workbook: ExcelScript.Workbook) {
//Assign the source variable to the selected range
let source: ExcelScript.Range = workbook.getSelectedRange()
//Assign the archive variable to the Archive worksheet
let archive: ExcelScript.Worksheet = workbook.getWorksheet("Archive - ATL staff only");
//Set the destination range in the archive sheet
let destination: ExcelScript.Range = archive.getRange("A:A").getExtendedRange(ExcelScript.KeyboardDirection.up).getLastCell().getOffsetRange(1,0)
//Determine if the offset range is set to A2 in the Archive sheet.
//If so, determine if A1 has a value. If it does not, update the
//destination range to cell A1.
if (destination.getAddress() === "'Archive - ATL staff only'!A2" ){
if (destination.getOffsetRange(-1,0).getValue() === "") {
destination = destination.getOffsetRange(-1,0)
}
}
//Copy the selected source range to the destination range
destination.copyFrom(source, ExcelScript.RangeCopyType.all);
//Delete the data in the source range
source.delete(ExcelScript.DeleteShiftDirection.up);
}
This code determines the last cell based on the data in column A in the archive sheet. This can be useful if the used range of the archive sheet has data somewhere, but column A does not. In this scenario, this code would still update correctly.
Also, this code doesn't copy and delete the entire row. So if you intend to use a selection smaller than the entire row, this code may work better.
I'm building a dynamic validation list, the missing ingredient is the following
I need to populate a cell (A1) with the contents of the last modified cell in a workbook.
I dont know the column/row/sheet of the cell i want to retrieve its content. So basically it is the last entry, and it can be anywhere in the workbook
Any chance of doing that?
Assuming that data will be stored in sheet named lastModifiedRange
function onEdit(e) {
const sheet=SpreadsheetApp.getActiveSpreadsheet().getSheetByName('lastModifiedRange')
sheet.getRange('A1').setValue(e.value);
sheet.getRange('A2').setValue(e.source.getActiveSheet().getName());
sheet.getRange('A3').setValue(e.source.getActiveRange().getA1Notation());
sheet.getRange('A4').setValue(e.oldValue);
}
edit : if you want to write it in the same sheet
function onEdit(e) {
const sheet=e.source.getActiveSheet()
sheet.getRange('A1').setValue(e.value);
}
I'm using the OpenXML API to read an excel file. I have a wrapper for the API, so I can directly read the rows and cells in Excel. There are two columns whose values are generated using a formula as follows.
=B12&"."&C12&"#dtestorg.com"
Basically, what it does is - create the email address using the first name and last name cell values for a user.
When I read this cell as follows, I read it as empty:
row.XCells[6].GetValue()
Is there a specific way to read the cells generated using formulae, in OpenXML?
Essential XlsIO is an option for reading & modifying Excel documents with formulas.
For example, let us consider you have an Excel sheet as shown below.
Step 1: Create a console application
Step 2: Add reference to Syncfusion.XlsIO.Base and Syncfusion.Compression.Base assemblies. These assemblies can be downloaded from NuGet too.
Step 3: Copy & paste the following code in the main method
//Create an instance of Excel application instance
using (ExcelEngine engine = new ExcelEngine())
{
//Open an existing Excel workbook
IWorkbook workbook = engine.Excel.Workbooks.Open(#"..\..\Template.xlsx");
//Access the worksheet with the name "Sheet1"
IWorksheet worksheet = workbook.Worksheets["Sheet1"];
//Access the range/cell "C2" from the worksheet
IRange range = worksheet["C2"];
//Access the Text to be displayed in the cell
Console.WriteLine("Display Text: " + range.DisplayText);
//Access the Formula of the cell
Console.WriteLine("Formula:" + range.Formula);
}
The whole suite of controls is available for free (commercial applications also) through the community license program if you qualify. The community license is the full product with no limitations or watermarks.
Note: I work for Syncfusion.
I need to generate a code in Matlab to Edit and then save an Excel file. The code has to do the following:
Open the Excel file
Go to the second sheet (sheet2)
Change the value of cell A2
Go to the first sheet (sheet1)
Save the sheet1 as a tab delimited text file (.txt)
Sheet 1 contains cells which depend on the cell A2 in sheet2 (I need to do this several times for different values of sheet2-A2, that's why I need to code it).
Browsing the web, I have been able to code steps 1-4 (see code bellow). However, I have not been able to find the way to do 5. I would very much appreciate your help. Thank you!!
% Open Excel Server:
Excel = actxserver('Excel.Application');
% Makes Excel visible in the screen:
set(Excel, 'Visible', 1);
% Open Excel file:
Workbooks = Excel.Workbooks.Open('E:\TEST.xlsx');
% Make the second sheet active:
Sheets = Excel.ActiveWorkBook.Sheets;
sheet2 = get(Sheets, 'Item', 2);
invoke(sheet2, 'Activate');
% Get a handle in the active sheet:
Activesheet = Excel.Activesheet;
% Edit cell A2:
A = 2;
ActivesheetRange = get(Activesheet,'Range','A2');
set(ActivesheetRange, 'Value', A);
pause(5) %Pauses the code for 5 seconds (so sheet 1 updates the formulas)
% Going to Sheet1 where the focal data is:
Sheets = Excel.ActiveWorkBook.Sheets;
sheet1 = get(Sheets, 'Item', 1);
invoke(sheet1, 'Activate');
For this kind of stuff, you can directly go to the Microsoft docs.
These should help you out:
http://msdn.microsoft.com/en-us/library/office/ff841185.aspx
http://msdn.microsoft.com/en-us/library/office/ff198017.aspx
Basically, you can issue the commands here, as you would in the SaveAs dialog in the Excel GUI.
I'm using EPPLUS to build custom Excel spread sheets. One functionality that I'm missing is to change the name of a cell. Is this somehow possible with EPPLUS? If not are there any other ways of doing it?
Suppose that book is your workbook and sheet is your worksheet. Let try this code snippet
var cell = sheet.Cells["C2"]; // Cell or Range you want to name
sheet.Names.Add("The_Name_Here", cell);
The name will be added to sheet. If you want to access the name in the whole workbook, try:
var cell = sheet.Cells["C2"]; // Cell or Range you want to name
book.Names.Add("The_Name_Here", cell);
Depend on your need the scope of the name can be sheet.Names or book.Names.
I don't know how to modify an existing name but sure that 1 cell/range may have multiple names.