Run Script (excel) in Power automate

AT_M 11 Reputation points
2021-02-15T02:56:43.65+00:00

Hi,

I created the sort script in excel online and connected to Power automate. It's set up with 'Recurrence' to run daily.
When I tested it as solo, it worked but it's failed when it's run by power automate. The error message was;
--- Quote ---
We were unable to run the script. Please try again.
Workbook not found.
clientRequestId: 0a43a19b-0b2c-4cfc-8ef9-3954a5767e0e
--- Unquote ---

I used workboog.getactiveworksheet method which is default behaviour in powerautomate.
Please advise 1) what is wrong about it and 2) is there any workaround this? Also, is there any method in script to clear table? Because, the table will be overlapped if I don't clear the table.

Look forward to hearing any idea on this.

Microsoft 365 and Office | Development | Office JavaScript API
{count} votes

6 answers

Sort by: Most helpful
  1. Carlos Felipe Lima Tecles 1 Reputation point
    2022-04-03T12:34:18.503+00:00

    I´m also with the same problem.

    Power Automate error:
    We were unable to run the script. Please try again.
    Runtime error: Line 18: Cannot read property '0' of null
    clientRequestId: 783d7250-a8a7-4770-baa6-2a3dd1e39223

    Script:
    function main(workbook: ExcelScript.Workbook) {
    let selectedSheet = workbook.getActiveWorksheet();

    // Ajusta Status
    selectedSheet.getRange("C2").setFormulaLocal("=SE(E2<=HOJE();\"ATRASADO\";SE(E2-7<=HOJE();\"SEMANA\";\"EM ANDAMENTO\"))");
    
    // Cola ajuste de status para outras linhas
    selectedSheet.getRange("C2").autoFill();
    
    // Formata colunas como data
    selectedSheet.getRange("D:D").setNumberFormatLocal("dd/mm/aa;@");
    selectedSheet.getRange("E:E").setNumberFormatLocal("dd/mm/aa;@");
    selectedSheet.getRange("F:F").setNumberFormatLocal("dd/mm/aa;@");
    selectedSheet.getRange("P:P").setNumberFormatLocal("dd/mm/aa;@");
    
    
    // Converte datas de texto para data de fato
    selectedSheet.getRange("D1").getResizedRange(0, selectedSheet.getRange("D:D").getValues()[0][0].toString().split(/[\t]/).length - 1).setValues([selectedSheet.getRange("D:D").getValues()[0][0].toString().split(/[\t]/)]);
    
    selectedSheet.getRange("E1").getResizedRange(0, selectedSheet.getRange("E:E").getValues()[0][0].toString().split(/[\t]/).length - 1).setValues([selectedSheet.getRange("E:E").getValues()[0][0].toString().split(/[\t]/)]);
    
    selectedSheet.getRange("P1").getResizedRange(0, selectedSheet.getRange("P:P").getValues()[0][0].toString().split(/[\t]/).length - 1).setValues([selectedSheet.getRange("P:P").getValues()[0][0].toString().split(/[\t]/)]);
    
    // Formata coluna de matrícula para que apareçam todos os dígitos
    selectedSheet.getRange("G:G").setNumberFormatLocal("0");
    
    // Formata coluna de CNPJ para que apareçam todos os dígitos
    selectedSheet.getRange("X:X").setNumberFormatLocal("0");
    
    // Formata colunas de CPF para que apareçam todos os dígitos
    selectedSheet.getRange("I:I").setNumberFormatLocal("0");
    selectedSheet.getRange("N:N").setNumberFormatLocal("0");
    
    // Dias Atraso
    selectedSheet.getRange("AD1:AD2").setFormulasLocal([["DIAS_ATRASO"], ["=se(C2=\"ATRASADO\";HOJE()-E2;\"\")"]]);
    
    // Teste CPF e idade RF
    selectedSheet.getRange("AE1:AF2").setFormulasLocal([["TESTE_CPF_RF", "TESTE_IDADE_RF"], ["=SE(AC2=\"OUTROS\";SE(I2=N2;\"ERRO\";\"\");\"\")", "=SE(AC2=\"OUTROS\";SE(HOJE()-P2>365*60;\"IDADE ACIMA 60\";\"\");\"\")"]]);
    

    }


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.