Forum Discussion

Rozz246's avatar
Rozz246
Copper Contributor
Jun 12, 2023

Excel Automate Script (Add Password)

Hello, 

I have created a simple Power Query to combine a number of tabs together. I would also like users to be able to refresh when they update data. But I also want to Password Protect the Combined Data Tab. 

I have recorded a script to refresh, but I don't know where I should add the Password into the Script below, can anyone offer a solution. 

 

Many Thanks, Sandra

 

function main(workbook: ExcelScript.Workbook) {
  let selectedSheet = workbook.getActiveWorksheet();
  // Unprotect sheet Combined List
  selectedSheet.getProtection().unprotect();
  // Refresh all data connections
  workbook.refreshAllDataConnections();
  // Protect sheet Combined List. Passwords are not recorded. Add a password below if needed
  selectedSheet.getProtection().protect({ allowAutoFilter: false, allowDeleteColumns: false, allowDeleteRows: false,
            allowEditObjects: true, allowEditScenarios: true, allowFormatCells: false,
            allowFormatColumns: false, allowFormatRows: false, allowInsertColumns: false,
            allowInsertHyperlinks: false, allowInsertRows: false, allowPivotTables: false,
            allowSort: false, selectionMode: ExcelScript.ProtectionSelectionMode.normal }, "");
}

1 Reply

  • JKPieterse's avatar
    JKPieterse
    Silver Contributor

    Rozz246 That is very simple:

    function main(workbook: ExcelScript.Workbook) {
      let selectedSheet = workbook.getActiveWorksheet();
      // Unprotect sheet Combined List
      selectedSheet.getProtection().unprotect("Password");
      // Refresh all data connections
      workbook.refreshAllDataConnections();
      // Protect sheet Combined List. Passwords are not recorded. Add a password below if needed
      selectedSheet.getProtection().protect({ allowAutoFilter: false, allowDeleteColumns: false, allowDeleteRows: false,
                allowEditObjects: true, allowEditScenarios: true, allowFormatCells: false,
                allowFormatColumns: false, allowFormatRows: false, allowInsertColumns: false,
                allowInsertHyperlinks: false, allowInsertRows: false, allowPivotTables: false,
                allowSort: false, selectionMode: ExcelScript.ProtectionSelectionMode.normal }, "Password");
    }

Resources