Skip to main content

Notifications

Community site session details

Community site session details

Session Id : amWNaSys3+cQeTnKANeLzE
Power Automate - General Discussion
Answered

Running MS Office Script on SharePoint spreadsheet leaving the file locked?

Like (0) ShareShare
ReportReport
Posted on 11 Oct 2021 15:54:25 by 78

Running MS Office Script on SharePoint spreadsheet leaving the file locked?

 

I have a scenario like this:

1. File is dropped in SP folder - triggers the flow with new file

2. File properties are grabbed to run MS Office Script on the data

3. If the script returns data in bad state, then move the file to a new SP folder --> This part is failing with file is in locked mode.

4. Otherwise create a list item with the read details from spreadsheet

 

I've tripled checked to make sure the file is not open in any browser windows. I am using the file currently for testing, so it's very unlikely someone else has the file open at all.

 

I've added a Do Until to wait 5 minutes for the file to unlock, but either that hasn't been long enough for the file to unlock or the file will not unlock.

 

My question is what is causing the file to lock in the first place? Could it be the run of the MS Office Script on the file?

I am able to open and edit the file as Expected after the run totally fails, so really drawing a blank with this.

 

This is the script - any advice for improvement is appreciated. 

 

younceb_0-1633967563166.pngyounceb_1-1633967572374.png

 

 

 

function main(workbook: ExcelScript.Workbook) : string {


let returnMessage = ""
 //Static name of the worksheet containing the concerned rows
let sheetRSS = workbook.getWorksheet("Sheet Name");

 //grab the table object, only one in the RSS template on this worksheet
let table = sheetRSS.getTables()[0];

 //get the used range
let tableRange = table.getRange()

//Reason for Request in column 2, 0 indexed array
let complexityColumn = tableRange.getColumn(5);

//Variable to inspect the contents of cell in loop
let currentCellValue = "";

//Number of cells in the Reason for Request column
let complexityColCellCount = complexityColumn.getCellCount();

//Tally the number of Create Package rows
let numberOfCreatePackageCells = 0


 //These values are associated with columns
 let currentProjectID_Cell = sheetRSS.getCell(4,2).getText().trim();
 //console.log("Project id: " + currentProjectID_Cell)

 let currentSite_Cell = sheetRSS.getCell(4, 5).getText().trim();
 //console.log("Site: " + currentSite_Cell)

 let currentDivision_Cell = sheetRSS.getCell(5, 5).getText().trim();
 //console.log("Division: " + currentDivision_Cell)

 let currentDepartment_Cell = sheetRSS.getCell(6, 5).getText().trim();
 //console.log("Dept: " + currentDepartment_Cell)

 let currentArea_Cell = sheetRSS.getCell(7, 5).getText().trim();;
 //console.log("Area: " + currentArea_Cell)

 //If any of these are empty, fail fast 
 if (currentArea_Cell === "" || currentProjectID_Cell === ""){
 returnMessage += "Error - One of the below required fields is empty in sheet: " + workbook.getName() +". /n Project ID: " + currentProjectID_Cell + "\nSite: " + currentSite_Cell + "\nDivision: " + currentDivision_Cell + "\nDepartment: " + currentDepartment_Cell + "\nArea: " + currentArea_Cell
 console.log(returnMessage)
 return returnMessage;
 }

 //Basically we have an array that will hold our rows
 //which is also an array - of objects
 let rows : Array<Array<Object>> = [];

 //Row object with needed values
 let row : Array<Object> = [];
 

//Loop through every cell in the Complexity column
for (let i = 0; i < complexityColCellCount; i++) {

 //Read the value of the cell in this spot based on loop counter
 currentCellValue = complexityColumn.getCell(i,0).getText();

 //found one?? Increment the counter and push the row values
 if (currentCellValue.includes("Create Package")){
 numberOfCreatePackageCells++;

 row = [{
 name: "DatabaseID",
 value: tableRange.getCell(i, 0).getText().trim(),
 required: true
 },
 {
 name: "Priority",
 value: tableRange.getCell(i, 3).getText().trim(),
 required: true
 },
 {
 name: "ReasonForRequest",
 value: tableRange.getCell(i, 2).getText().trim(),
 required: true
 },
 ...
 ]
 rows.push(row)
 }
 }


 //If we never found a Create Package row, then we have hit an error scenario
 //No work to do
 if(numberOfCreatePackageCells = 0){

 returnMessage = "Error - No Create Package Rows found in file."
 console.log(returnMessage);
 return returnMessage;
 
 }

 //Check each row has the value populated for fields marked required as true
 for(let b = 0; b < rows.length; b++){
 for (let c = 0; c < rows[b].length; c++){
 let currentArray = rows[b];
 let currentObj : Object = currentArray[c];
 if(currentObj["required"] === true)
 if (currentObj["value"] === ""){
 returnMessage += "\n" + "Error - " + currentObj["name"] + " was found to be empty when required is marked as: " + currentObj["required"]+ " in RSS Spreadsheet: " + workbook.getName(); 
 }
 }
 }

 if (!returnMessage.includes("Error")){
 returnMessage = JSON.stringify(rows);
 }
 console.log(returnMessage);
 return returnMessage;
}

 

 

  • Verified answer
    younceb Profile Picture
    78 on 12 Oct 2021 at 12:35:31
    Re: Running MS Office Script on SharePoint spreadsheet leaving the file locked?

    Although I am not totally sure the cause of the lock - it seems like it's the Office Script call.

     

    I was able to get the flow to progress by waiting 10 minutes or so, up from the 5 minutes I was waiting before.

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

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Congratulations to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Power Automate - General Discussion

#1
David_MA Profile Picture

David_MA 8 Super User 2025 Season 1

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 8 Super User 2025 Season 1

#3
ankit_singhal Profile Picture

ankit_singhal 7 Super User 2025 Season 1

Overall leaderboard
Loading started
Loading complete