Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Automate - Using Flows
Answered

Get Last Line with Office Scripts

(0) ShareShare
ReportReport
Posted on by 67

I have a .xlsx with a lot of rows of information, I wonder how can I get the last line of this document? 

 

I'm supposed to be adding more lines to the end of the archive, every month. There is a sample of the script I'm working on:

 

function main(workbook: ExcelScript.Workbook) {
 let selectedSheet = workbook.getWorksheet("sheet");
 return JSON.stringify(selectedSheet.getRange());
}

 

Also tried with: 

return JSON.stringify(selectedSheet.getRange().getVisibleView().getValues());
 
But I'm getting the error:
 
Converting circular structure to JSON
--> starting at object with constructor 't'
| property 'm_trackedObjects' -> object with constructor 'e'
--- property 'm_context' closes the circle
clientRequestId: 986aa897-d022-43a5-b32e-672524deb8c8
 
Maybe you can help me with this, thanks!

 

  • victor_lion Profile Picture
    67 on at
    Re: Get Last Line with Office Scripts

    Yes @Yutao it works! Thanks! 

  • Verified answer
    Yutao Huang Profile Picture
    Microsoft Employee on at
    Re: Get Last Line with Office Scripts

    @victor_lion - it's great to hear you've found the solution! Wondering if you could also try selectedSheet.getUsedRange(true).getRowCount(), which might be a bit more efficient.

  • victor_lion Profile Picture
    67 on at
    Re: Get Last Line with Office Scripts

    Thank you @Yutao I've solved this problem using the following script:

     let columnValues = selectedSheet.getUsedRange(true).getColumn(column).getValues();
     return columnValues;

     

    And by this I get an array, so by this I know the length of the file, and can get the last line.

  • Yutao Huang Profile Picture
    Microsoft Employee on at
    Re: Get Last Line with Office Scripts

    Hey @victor_lion ,

     

    Wondering if you have tried Range.getSurroundingRegion( ) . I think that should work as long as there are no completely blank rows or blank columns within the range.

     

    https://docs.microsoft.com/en-us/javascript/api/office-scripts/excelscript/excelscript.range?view=office-scripts#getSurroundingRegion__

     

    -Yutao

  • victor_lion Profile Picture
    67 on at
    Re: Get Last Line with Office Scripts

    Hi, I'm looking to a function like .getUsedRange(), as I am no trying to add more content to the file. 

  • DamoBird365 Profile Picture
    8,942 Microsoft Employee on at
    Re: Get Last Line with Office Scripts

    Hi @victor_lion 

     

    You could probably get away without using Office Scripts and do something like https://www.exceltip.com/lookup-formulas/find-the-last-row-of-data-in-excel.html 

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Markus Franz – Community Spotlight

We are honored to recognize Markus Franz as our April 2025 Community…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 146,552 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 65,928 Most Valuable Professional

Leaderboard

Featured topics

Restore a deleted flow