Creating a loop to rename all sheets in workbook using excel office script - office-scripts

This is the code:
/**
* This sample renames a worksheet from "Sheet1" to "SALES".
*/
function main(workbook: ExcelScript.Workbook) {
// Get a worksheet named "Sheet1".
const sheet = workbook.getWorksheet('Sheet1');
// Set its name to SALES.
sheet.setName('SALES');
// Get a worksheet named "SALES".
//const sheet = workbook.getWorksheet('SALES');
// Position the worksheet at the beginning of the workbook.
sheet.setPosition(0);
}

You can use this code to rename all of the sheets in the file using a convention like sales01, sales02, etc.
function main(workbook: ExcelScript.Workbook) {
let worksheets : ExcelScript.Worksheet[] = workbook.getWorksheets()
let sheetsCount: number = worksheets.length
for (let i = 0; i < sheetsCount; i++) {
worksheets[i].setName(`SALES0${i + 1}`)
}
}

Related

Adding a second sheet & saving it doesn't show in Excel

I have 2 datable and I added for loop to create the sheet and save it, but when I open excel file I can see only one(1st) sheet.
Please check why 2nd is not added in excel.
Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Excel.Workbook wb = xlApp.Worksheets.Add();
Excel.Worksheet ws = null;
Excel.Range chartRange =null;
for(int i=0; i<dataset.Tables.Count; i++)
{
// here I am creating new sheet for each datatable but only 1st datatable value I can see in
XL, 2nd sheet is not created
ws = (Excel.Worksheet)worksheets.Add();
ws.Name = dataset.Table[i].TableName;
//business logic here to assign data
var dgarray = new object[rows.count, columns.count];
//for loop to assign column name
dgarray[0,c] = column name;
//for loop to assign row value
dgarray[rowindex, colindex] = row value;
int rowcount = dgarray.getlength(0);
int columscount = dgarray.getlength(1);
chartRange = (Excel.Range)ws.cells[1,1];
chartRange = chartRange.get_Resize(rowcount,colcount);
chartRange.set_Value(Excel.XlRangeValueDatatype.XlRangeValueDefault,dgarray);
}
((Excel.Worksheet).ws).SaveAs(filename);

How to keep the background color when you edit the excel sheet by POI

I code in C#. I use NPOI(version 2.5.5). But I link this OP to Apache POI as well because the solution for this would be the same for both modules.
I have this excel file whose extention is .xlsx:
I editted the Excel sheet via NPOI.
code:
private void button_editByNpoi_Click(object sender, EventArgs e)
{
string filePath = destFileName;
IWorkbook workbook;
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
workbook = new XSSFWorkbook(fs);
}
/*
//1枚目のシートを取得
ISheet sheet = workbook.GetSheetAt(0);
//セルを取得
IRow row = sheet.GetRow(1);
ICell cell = row.GetCell(1);
//セルに書き込み
//cell.SetCellValue("a");
*/
using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
//上書き保存する。
workbook.Write(fs);
}
}
As the result, I got this excel sheet:
the light gray color has gone after editting. How can I keep the color?

How to add a new row at the beginning of a existing excel sheet using exceljs

It adds at the end of the sheet. I want to add at the beginning. How can I do it using exceljs
workbook.xlsx.readFile(nameFileExcel)
.then(function() {
var worksheet = workbook.getWorksheet(2);
var lastRow = worksheet.lastRow;
var getRowInsert = worksheet.getRow(++(lastRow.number));
getRowInsert.getCell('A').value = 'yo';
getRowInsert.commit();
return workbook.xlsx.writeFile(nameFileExcel);
});

Emulate simple VBA macro to be compatible with Google Sheets

How can I convert this simple VBA to Google Sheets?
Sub SELECT()
Dim Cval As Variant
Cval = Sheet4.Range("A16").Value
Sheet4.Range("D1:T" & Cval).Select
End Sub
I keep getting errors while trying to use this macro in Google Sheets and I cannot find the issue.
The code I'm currently working on in Google Sheets:
function SELECTIE() {
var spreadsheet = SpreadsheetApp.getActive();
var Cval = spreadsheet.getRange("A16").Value
spreadsheet.getRange("D1:T" & Cval).activate();
};
You only have one minor issue.
In the first statement, you're getting the spreadsheet object, but you need the sheet object. So getSheets()[0] will get you the first worksheet.
function SELECTIE() {
var ss = SpreadsheetApp.getActive().getSheets()[0];
var cVal = ss.getRange('A16').getValue();
ss.getRange('D1:T' + cVal).activate();
}

COM exception while selecting cells in excel with c#

I have a problem selecting a cell in excel using c# while running through a "for" loop.
I have 3 datatables in a dataset(dsReportGrid) which I'm exporting to excel (one sheet per table).
When I try to select a range so I can freeze panes, it goes well in the first iteration of the loop (dsReportGrid.table[0]), but when it gets to second iteration when I try to select the cell in the secondsheet it throws me a COM exception on the select event (the code line with two asterisks).
Why is it happening and how can I handle it?
Thanks,
private void saveXlS()
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range xlCells;
Excel.Range freezeCell;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);
for (int table = 0; table <= dsReportGrid.Tables.Count -1; table++)
{
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(table +1);
Excel.Range k = xlWorkSheet.get_Range("a2");
**k.Select();**
k.Application.ActiveWindow.FreezePanes = true;
break;
}
...rest of code writing from dataset to excel
}
Try activating the worksheet before attempting to select the range. I think you can only select a range on an active worksheet.

Resources