Working with Excel data not in table format - excel

Scenario, Email received with Excel file (.xls) attached, file contains data (Sheet1) that is not formatted as a table.
Q:
Is there some way using Power Automate to run a script against it to apply transforms?
Ideal outcome would be:
Excel file received as email attachment, delete header rows in Sheet1 (rows 1 and 2), save as CSV file and email new file to user.

Thank you for posting the question.
There are 3 parts to it.
Extracting the email attachment (xls) and saving it as xlsx. Converting xls > xlsx may be possible - but I'm not sure about the exact steps. Are you sure you are getting xls and not xlsx?
Removing header row is possible with Office Scripts. You can try recording your action in Excel for web and use that script. Be sure to change the sheet name as shown below.
function main(workbook: ExcelScript.Workbook) {
let sheet = workbook.getWorksheet('Sheet1');
// This can also be first sheet if you know it'll always be first sheet that you want to operate on.
// let sheet = workbook.getFirstWorksheet();
sheet.getRange("1:2").delete(ExcelScript.DeleteShiftDirection.up);
}
To export as CSV, there seems to be online resources to do refer to: https://www.youtube.com/watch?v=Ik-TLaMByhw&ab_channel=AndersJensen
Good luck and let us know how it worked out.

Related

How do I script the name of the excel online file to a cell?

I'm completely new at scripting.
Excel online - I have created a bunch of new excel files in Powerautomate, but I need the file specific filename inside each document. How do I do this?
I have found - console.log(workbook.getName()), this give me the correct "output", but I cant write it to a cell.
It is cell "B2", in spreadsheet "Data".
Hope you are able to help,
Thanks
Edit:
I have a pile of excel files that have been generated via MS Powerautomate.
Powerautomate is generating a specific name for each file. I need this file name inside each excel file, to do a specific filtering in a bunch of data.
The following picture are in Danish.
screenshot of execl file
The name of this document is "Skabelon" and I would like to have this name automatically filled in spreadsheet "Data" "B2".
Is this possible?
//Mads

Automatically updating csv file in sheets

I have a csv file in my PC that is updating every hour. I tried to automatize the update in google sheets too.
This was the formula I used
=IMPORTDATA("C:\Users\octav\Documentos\Movers\RD\ALGO-USD.csv")
I got the error code #N/A
Is this something to be with the formula parameters? bc it ask me for the url and i´m putting the filepath
Can someone tell me if I have something wrong with my formula or what I´m trying is just not something possible in sheets?
When I saw your Spreadsheet, I found the following formula.
=IMPORTDATA("https://drive.google.com/file/d/###/view?usp=sharing")
In this case, even when the file is the CSV data, HTML is returned. I thought that this might be the reason for your issue. In order to avoid this issue, how about the following patterns? I thought that one of the following 2 patterns might be able to be used for your situation.
Pattern 1:
In this pattern, the CSV data is loaded using the formula IMPORTDATA. For this, please do the following flow.
Publicly share the CSV file.
Put a formula of =IMPORTDATA("https://drive.google.com/uc?export=download&id=###").
Please replace ### to your file ID of CSV file.
Pattern 2:
In this pattern, the CSV data is loaded using Google Apps Script. Please copy and paste the following script to the script editor of Spreadsheet. And, please set the file ID of your CSV file and run myFunction. By this, the CSV data is put to the sheet Sheet1. When you want to change the sheet, please modify Sheet1.
function myFunction() {
const fileId = "###"; // Please set the file ID of CSV file on your Google Drive.
const csv = DriveApp.getFileById(fileId).getBlob().getDataAsString();
const values = Utilities.parseCsv(csv);
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
sheet.getRange(1, 1, values.length, values[0].length).setValues(values);
}
References:
parseCsv(csv)
setValues(values)

Excel (xlsx) file from GMAIL to Google Sheets - Copy As Values Not Formula Errors

My question is very similar to this one, except I have formulas in my excel file which means all the data ends up being #error.
Importing XLSX file from the monthly e-mail (in Gmail) to the designated Google Sheet
Recap, I get an excel by email which I need to convert to google sheet where I already have a script which runs to pull data and then upload stock values.
Everything works well except for this one source where there are formulas in the excel file.
There's a few formulas in the excel sheet but only one column (P) I really need which has the below formula in it. It looks up the stock id to another sheet that I don't have.
=IF(ISERROR(VLOOKUP(A2,'W:\Stock\Stockboard.xlsx]Stock'!$B:$N,13,FALSE)),0,VLOOKUP(A2,'W:\Stock\[Stockboard.xlsx]Stock'!$B:$N,13,FALSE))
Example File
The sheet opens in excel and excel understandably has the ability to not refresh and therefore not show errors. When I manually export to csv it's also fine.
This is the code at the moment, which is the same as the original post.
var thread = GmailApp.search("from:(email) Stock Update has:attachment",0,1)[0];
var message = thread.getMessages()[0];
var xlFile = message.getAttachments()[0];
var convertedSpreadsheetId = Drive.Files.insert({mimeType: MimeType.GOOGLE_SHEETS}, xlFile).id;
var sheet = SpreadsheetApp.openById(convertedSpreadsheetId).getSheets()[0]; // There is the data in 1st tab.
var data = sheet.getDataRange().getValues();
ss.getSheetByName("test").getRange(1,1,data.length, data[0].length).setValues(data);
Drive.Files.remove(convertedSpreadsheetId); // Remove the converted file.

Not able to convert Excel file to CSV

I have an Excel file with 2 sheets. One with my data with headers and the other is for references. It's basically a template where I am using Sheet 2 for reference values. When I save the file to CSV, it saves all garbage data which I think are due to 2 work sheets and the references between them.
Please find the attached excel sheet and let me know how I can do a proper conversion?
Notes:
I am using Office 2010 Standard
I have tried copying and pasting with "Values" option.
I have tried copying and pasting with "Merge Destination Formatting"
Nothing worked.
I was able to find the solution.
When you copy the selected range of records and paste them to New excel file, just drag the cursor some columns ahead. Remove the newly added columns (Not sure from where they come). Now do a Save As CSV. Thats it.

Use SSIS to populate Excel workbook generated using OOXML

We are trying to generate MS Excel workbook using OOXML and populate data using SSIS.
We are able to generate Workbook and sheets, also able to create columns and insert data in the Header cell. We can also populate data using SSIS.
But the Sheet (DocumentFormat.OpenXml.Spreadsheet.Sheet) and all cells (DocumentFormat.OpenXml.Spreadsheet.Cell) becomes OpenXmlUnknownElement. So we are not able to read sheet / cell using following code: Sheet sheet = workbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == "Sheet1").SingleOrDefault<Sheet>();
We are able to read the same file if we first open it using MS Excel and save. Does anyone know how to resolve this?
You probably forgot to give your sheet a name. You can see this by using
Sheet sheet = workbookPart.Workbook.Descendants<Sheet>().FirstOrDefault
and you'll see that your sheet name is either undefined or garbage text.
If that does not help. Create a simple document in code save it in OOXML and open it in a xml viewer. then make a copy open it in Excel and save it and see the difference in xml. That is often a good start seeing what excel has added by default to the document.
Excel is very tolerant of things that you have done wrong when creating the document in code, and magically fix them when you open the document again.
A bad hack would be using interop to open the document saving it again in code. wich would fix everything for you.
Workbook wrkbk = app.Workbooks.Open(#"c:\del.xls");
wrkbk.Save();
wrkbk.Close();

Resources