Officescript used range issue - excel

I am new to Excel office scipt and not sure if there is a limitation running script on xls file vs xlsx file.
Below is my script which doesnt have an issue when I run on xlsx file.
However when I open a xls file it doesnt work and get error message "Line 6: Worksheet getUsedRange: There was an internal error while processing the request."
function main(workbook: ExcelScript.Workbook)
{
// Get the current worksheet
let ws = workbook.getActiveWorksheet();
//get active range of WorkSheet
let range = ws.getUsedRange();
// Create a table using the data range.
let newTable = ws.addTable(range, true);
newTable.setName("TableTKO");
}
I have been playing around and so far have observed the following:
when I save as the file from xls to xlsx it works
when I copy out the data from xls to a new spreadsheet (xlsx) it works
if I change the code and hardcode the range it will work
Any ideas why sometimes getting the used range work and is there an alternative of getting the last row/column with out using let range = ws.getUsedRange() as I dont want to hard code it?
Thanks

Related

How do I add a script to a Google Sheet to automatically take data from an XLSX file on Google Drive?

I'm trying to automatically update a Google Sheet from a separate XLSX file, since the XLSX file gets regularly updated, but I need to do some data cleaning. I tried doing a query and importrange neither of which can get data from an xlsx file.
It seems like I need to write a script on the Google Sheet to automatically take the data from the xlsx. Where do I add this, and how would I go about getting started? I have access to both files, so permissions shouldn't be an issue.
Suggestion: Temporarily Convert the Excel File to Google Sheets File to Extract Data
Unfortunately, there is no direct way to extract data from Excel files to Google Sheets using Google Apps Script. As a workaround, you need to first convert your excel file to Google Sheets and then extract the data from the converted file to your output Google Sheets file. You may use the following script as a basis for yours:
function importData() {
var xlsxName = "Test 1.xlsx"; //Change source file name accordingly
var convertID = convert(xlsxName).toString();
var xLSX = SpreadsheetApp.openById(convertID).getSheetByName("Input");
var ss = SpreadsheetApp.openById("<output Sheet ID>").getSheetByName("Output"); //Change output sheet ID
var lastColumn = xLSX.getLastColumn();
var lastRow = xLSX.getLastRow();
ss.getRange(1, 1, lastRow, lastColumn).setValues(xLSX.getDataRange().getValues()); //Sets values from converted xlsx data to output sheet
DriveApp.getFileById(convertID).setTrashed(true); //deletes temporary file
}
function convert(excelFileName) {
var files = DriveApp.getFilesByName(excelFileName);
var excelFile = (files.hasNext()) ? files.next() : null;
var blob = excelFile.getBlob();
var config = {
title: "[Converted File] " + excelFile.getName(), //sets the title of the converted file
parents: [{ id: excelFile.getParents().next().getId() }],
mimeType: MimeType.GOOGLE_SHEETS
};
var spreadsheet = Drive.Files.insert(config, blob);
return (spreadsheet.id); //Returns the ID of the converted file
}
This script involves:
Converting the Excel file to a temporary Google Sheets file.
Importing the data from the temporary Google Sheets file to the desired/output Google Sheets file.
Deleting the temporary Google Sheets file.
NOTE:
Expect a longer runtime when applying this script to a bigger excel file.
You may modify the script to be suitable for your current issue.
The script should be added to your desired output Google Sheets.
Do not forget to add the Drive API service to your script.
Sample Test Case:
Input:
Expected Output:

How to update the source data range for existing chart

I'm trying to write a script that finds data in a sheet dynamically (the dimensions of the table need to be flexible in both axis) and then updates the source data range for an existing chart on another sheet (so that my users don't need to set up the styling themselves).
Below is my script so far. Everything works apart from the final line where Excel Online gives me the error:
"Line 10: Chart setData: You cannot perform the requested operation"
function main(workbook: ExcelScript.Workbook)
{
let selectedSheet = workbook.getWorksheet("Enter data in this sheet");
// Add a new table at used range on selectedSheet
let range = selectedSheet.getUsedRange();
if(selectedSheet.getTables().length == 0)
{
let newTable = workbook.addTable(range, true);
}
workbook.getWorksheet("The Chart").getChart("Chart 1").setData(range);
}
The worksheet names are correct and so is the chart name as far as I can see:
screenshot of excel online showing chart and worksheet names
Answer found; the chart was on a protected sheet which did not allow editing of objects. Updating the protection to allow all users to edit objects has resolved the issue.

Openpyxl created excel file with table causes file that requires recovery error

I have been testing adding a table to a worksheet using openpyxl, but I get the error below when I try to open it. The file opens, but the formatting isn't correct. After hitting recover, excel reports that there was an issue with the table xml. Is there a workaround/fix for this?
The code I'm using:
import openpyxl
from openpyxl import Workbook
from openpyxl.worksheet.table import Table, TableStyleInfo
xl_file_name = "new_test.xlsx"
wb = Workbook()
ws = wb.worksheets[0]
ws.title = "Table_Sheet"
headers = ["header1","header2","header3"]
for col in range(1,len(headers)+1):
for row in range(1,5):
if row == 1:
ws.cell(row,col).value = headers[col-1]
else:
ws.cell(row,col).value = str(row)
tbl = Table(displayName="Tbl1",ref="A1:C4")
style = TableStyleInfo(name="TableStyleMedium9", showFirstColumn=False, showLastColumn=False, showRowStripes=True, showColumnStripes=True)
tbl.tableStyleInfo = style
ws.add_table(tbl)
wb.save("new_test.xlsx")
Your name for the table is causing the problem. Run the same code with displayName="Tbl" or displayName="Tbl_1" instead, and you'll see it works fine. I'm not 100% sure, but I think the cause of the issue is that the name you give conflicts with the formatting for a possible cell reference of TBL1.
For me the following worked:
Change the Workbook as you wish (only Data no formatting)
Save the Workbook (If you would try to open it here it will display the error message)
Close the Workbook
Open the Workbook again (I think here Excel fixes the issue automatically)
Insert necessary formatting commands
Save the workbook
Close the Workbook
Or, as code:
import openpyxl
workbook = openpyxl.load_workbook(Source_Path)
##your code appending and deleting values - which I think sometimes causes the errors
workbook.save(Destination_Path)
workbook.close
#Now open it again
workbook = openpyxl.load_workbook(Destination_Path)
#Your Code to format
workbook.save(Destination_Path)
workbook.close
Now you should be able to open the Excel file without an error.
I've had the same error message.
I was creating tables with numbers at the start of the name, so I changed that code to add t_ at the beginning, so
table_name = "112MHZ_data"
became
table_name = "t_112MHZ_data"
And that solved it for me.

Hidden Cells still showing in an OfficeWriter Excel Spreadsheet

When my Office Writer Excel report opens, it randomly un-hides some of the hidden cells and columns. I have verified that it is not the data that causes the columns or cells to not be hidden. Has anyone experienced this before and is there a way to make sure that all columns or cells stay hidden when the excel file is opened?
I work for SoftArtisans. We have not had any other reports of programmatically hidden columns becoming visible in the output file. We also have not been able to reproduce the behavior you are reporting. It would be helpful to see a code snippet, as well as to know which version of OfficeWriter you are using and which version of Excel is being used to open the output file.
There are two ways to hide columns with our API, both using the ColumnProperties object. You can set the hidden property to true or set the width property to zero. You could do both if you like, although that shouldn't be necessary.
For example:
ExcelApplication xla = new ExcelApplication();
Workbook wb = xla.Create(ExcelApplication.FileFormat.Xlsx);
//or if opening an existing workbook
//Workbook wb = xla.Open(inputFilePath);
//Get a handle on the worksheet
Worksheet ws = wb.Worksheets[0];
//Write a value to a cell
ws.Cells[0, 9].Value = "Hidden Value";
//Get a handle on the column you want to hide
ColumnProperties colProps = ws.GetColumnProperties(9);
//set the column to hidden
colProps.Hidden = true;
//or set the column width to zero
colProps.Width = 0;
//Stream the output file to the response
xla.Save(wb, Page.Response, "HiddenColumnTest.xlsx", false);

How to add more than 3 sheets to an excel workbook from within MATLAB

How do I add more sheets to an excel workbook from within matlab?
I set up the workbook like so (based on code I got from someone else's post in this forum):
%# create Excel COM Server
Excel = actxserver('Excel.Application');
Excel.Visible = true;
%# create new XLS file
wb = Excel.Workbooks.Add();
wsheet=1;
wb.Sheets.Item(wsheet).Activate();
That's fine. Then later on inside the loop I open a new sheet after so many loops:
...
if loop==sheetlimit,
wsheet=wsheet+1;
wb.Sheets.Item(wsheet).Activate();
end
This works up to sheet 3. But when wsheet=4 I get this error message:
??? Invoke Error, Dispatch Exception: Invalid index.
Error in ==> filename at 97
wb.Sheets.Item(wsheet).Activate();
Appreciate any help. Thanks.
I don't know Matlab but I would be surprised if wb.Sheets.Item(wsheet).Activate(); is actually adding any new worksheets. Most likely it is selecting / activating each worksheet in your wb workbook and your default Excel template has three worksheets. Hence why it errors when it gets to more than three.
Something like this might add a new Excel worksheet:
wb.sheets.Add();
Aargh - comment formatting completely messed up - I'll re-enter it as an new answer
Yes wb.sheets.Add(); will work. You can query the available methods of an interface like this:
methods(wb.sheets)
which gives:
Methods for class Interface.000208D7_0000_0000_C000_000000000046:
Add FillAcrossSheets PrintOut addproperty events loadobj set
Copy Item PrintPreview delete get release
Delete Move Select deleteproperty invoke saveobj

Resources