{"id":6522,"date":"2021-07-07T08:42:25","date_gmt":"2021-07-07T15:42:25","guid":{"rendered":"https:\/\/officedevblogs.wpengine.com\/?p=6522"},"modified":"2022-05-31T09:10:01","modified_gmt":"2022-05-31T16:10:01","slug":"uniper-boosts-employee-productivity-using-office-scripts","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/uniper-boosts-employee-productivity-using-office-scripts\/","title":{"rendered":"Office Scripts help Uniper boost employee productivity"},"content":{"rendered":"<p><img decoding=\"async\" class=\"wp-image-6524 alignnone\" style=\"font-size: 1rem;\" src=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/07\/Gareth-uniper-150x150.png\" alt=\"Gareth Naylor\" width=\"91\" height=\"91\" srcset=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/07\/Gareth-uniper-150x150.png 150w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/07\/Gareth-uniper-24x24.png 24w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/07\/Gareth-uniper-48x48.png 48w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/07\/Gareth-uniper-96x96.png 96w\" sizes=\"(max-width: 91px) 100vw, 91px\" \/><\/p>\n<p>To learn more about Office Scripts and how to automate a report, today you\u2019ll hear from Gareth Naylor, a Group Wide Architect\/Strategist in the CIO Office from Uniper.<\/p>\n<h2>Build an Office Script<\/h2>\n<p>Before I started this project, I had never used Office Script before. I found the process straightforward thanks to the online documentation which helped me understand what is possible using scripts. As you can see in the code sample, the left-hand column of data was entirely automatically generated by the recorder. Having a low code solution readily available was very helpful.<\/p>\n<p>Using Office Scripts and Power Automate, I built a script that fully automates a report that used to take me at least one to two hours per month. Before using Office Scripts, I was spending time manually downloading data from email, formatting it in spreadsheet, and adding it to a Power BI report that gives statistics on the usage and top articles in our company intranet, IT Inside. Given IT Inside is the main IT resource for Uniper, this Power BI report is key to our goal of improving the style and content of articles that we publish.<\/p>\n<p>For me, writing my own solution was more challenging as it involved returning processed data back to Power Automate. However, with some support I wrote the code in the right-hand column without using the recorder. It is great to have the option of writing your own code or just recording it.<\/p>\n<p>After putting it all together and testing, I was able to put the Power Automate Flow into production. Since this the process has worked reliably and without any manual input and this ensures that the statistics are always up to date. Additionally, since creating this solution, I was asked to implement it for other areas of our Intranet. In total, the automated processes that I\u2019ve created with Office Scripts save us around 6 hours of effort per month and I have many colleagues, both in and out of IT, who are beginning to use Office Scripts to make their work lives easier.<\/p>\n<h2>Office Script samples<\/h2>\n<p>In total, I created three files using six scripts. To create these scripts, I used a combination of the Action Recorder, which records actions you take in the Excel workbook and saves them into a script; and the Code Editor, which enables you to write and edit scripts through TypeScript\/JavaScript.<\/p>\n<p><span class=\"TextRun SCXW70446975 BCX8\" lang=\"EN-GB\" xml:lang=\"EN-GB\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW70446975 BCX8\">The full process took about six steps:\u00a0<\/span><\/span><\/p>\n<ol>\n<li><span class=\"TextRun SCXW70446975 BCX8\" lang=\"EN-GB\" xml:lang=\"EN-GB\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW70446975 BCX8\">Using Power Automate, I downloaded CSV files from an email as .xlsx files. <\/span><\/span><\/li>\n<li><span class=\"TextRun SCXW70446975 BCX8\" lang=\"EN-GB\" xml:lang=\"EN-GB\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW70446975 BCX8\">I then used an Office Script for each file to organize the data into tables and add the publication month to the report. <\/span><\/span><\/li>\n<li><span class=\"TextRun SCXW70446975 BCX8\" lang=\"EN-GB\" xml:lang=\"EN-GB\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW70446975 BCX8\">Next, I <\/span><span class=\"NormalTextRun SCXW70446975 BCX8\">used Office Scripts to return multiple rows of each table as JSON to Power Automate. <\/span><\/span><\/li>\n<li><span class=\"TextRun SCXW70446975 BCX8\" lang=\"EN-GB\" xml:lang=\"EN-GB\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW70446975 BCX8\">Power Automate then appended the data to the<\/span><span class=\"NormalTextRun SCXW70446975 BCX8\"> Excel files. <\/span><\/span><\/li>\n<li><span class=\"TextRun SCXW70446975 BCX8\" lang=\"EN-GB\" xml:lang=\"EN-GB\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW70446975 BCX8\">I used Office Scripts to de-duplicate data in a file. <\/span><\/span><\/li>\n<li><span class=\"TextRun SCXW70446975 BCX8\" lang=\"EN-GB\" xml:lang=\"EN-GB\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW70446975 BCX8\">Finally, I used Power Automate to refresh the Power BI\u00a0<\/span><span class=\"NormalTextRun SCXW70446975 BCX8\">r<\/span><span class=\"NormalTextRun SCXW70446975 BCX8\">eport,\u00a0<\/span><span class=\"NormalTextRun SCXW70446975 BCX8\">and<\/span><span class=\"NormalTextRun SCXW70446975 BCX8\">\u00a0then<\/span><span class=\"NormalTextRun SCXW70446975 BCX8\">\u00a0<\/span><span class=\"NormalTextRun SCXW70446975 BCX8\">send a Teams message<\/span><span class=\"NormalTextRun SCXW70446975 BCX8\">\u00a0and email\u00a0<\/span><span class=\"NormalTextRun SCXW70446975 BCX8\">about the report.\u00a0<\/span><\/span><span class=\"EOP SCXW70446975 BCX8\" data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:257}\">\u00a0<\/span><\/li>\n<\/ol>\n<p>In the following code sample, you can see some of the code that was autogenerated using the Action Recorder as well as code I wrote with the Code Editor.<\/p>\n<pre class=\"lang: decode:true\" title=\"Autogenerated Office Scripts Code\">\/\/Autogenerated Office Scripts Code\r\n\r\nfunction main(workbook: ExcelScript.Workbook) { \r\n\r\n  let selectedSheet = workbook.getActiveWorksheet(); \r\n\r\n  \/\/ Set range E1 on selectedSheet \r\n\r\n  selectedSheet.getRange(\"E1\") \r\n\r\n    .setValue(\"ReportMonth\"); \r\n\r\n  \/\/ Set width of column(s) E1 to 70.5 \r\n\r\n  selectedSheet.getRange(\"E1\") \r\n\r\n    .getFormat() \r\n\r\n    .setColumnWidth(70.5); \r\n\r\n  \/\/ Create a table with format: 'Sheet1' \r\n\r\n  let table1 = workbook.addTable(selectedSheet.getUsedRange(true), true); \r\n\r\n  table1.setPredefinedTableStyle(\"TableStyleLight9\"); \r\n\r\n  \/\/ Set range E2 on selectedSheet \r\n\r\n  selectedSheet.getRange(\"E2\") \r\n\r\n    .setValue(\"=EDATE(TODAY(), -1)\"); \r\n\r\n  \/\/ Set number formats for Sheet1!E:E \r\n\r\n  selectedSheet.getRange(\"E:E\") \r\n\r\n    .setNumberFormatLocal(\"[$-en-GB]mm-yyyy;@\"); \r\n\r\n  \/\/ Set width of column(s) E1 to 261 \r\n\r\n  selectedSheet.getRange(\"E1\") \r\n\r\n    .getFormat() \r\n\r\n    .setColumnWidth(261); \r\n\r\n}<\/pre>\n<pre class=\"lang: decode:true\" title=\"Written Office Scripts code\">\/** \r\n * Written code (compare and contrast with the previous autogenerated code) \r\n * Lists rows present in a specific table named tableName and return to Power Automate as JSON \r\n * *\/ \r\n\r\nfunction main(workbook: ExcelScript.Workbook, tableName: string = \"Table1\") { \r\n\r\n    const table = workbook.getTable(tableName); \r\n\r\n    \/\/ Get the range of only the body of the table, not the header \r\n\r\n    const tableRange = table.getRangeBetweenHeaderAndTotal(); \r\n\r\n    const tableValues = tableRange.getValues(); \r\n\r\n    \/\/ We must specify the table headers or the rows won't show up nicely in Power Automate. We must also specify the headers below in the TypeScript type (... as { ... }). \r\n\r\n    const tableHeaders = [\"PageID\", \"Page\", \"ArticleID\", \"Article\", \"PublishedDate\", \"Views\", \"Unique Visits\", \"Users\", \"Likes\", \"Shares\", \"Comments\"]; \r\n\r\n    const result = tableValues.map((rowValues) =&gt; { \r\n\r\n        return rowValues.reduce((row, cellValue, i) =&gt; { \r\n\r\n            const header = tableHeaders[i]; \r\n\r\n            row[header] = cellValue; \r\n\r\n  \r\n\r\n            return row; \r\n\r\n        }, {} as { \r\n\r\n            A: string; \r\n\r\n            B: string; \r\n\r\n            C: string; \r\n\r\n            D: string; \r\n\r\n            E: string; \r\n\r\n            F: string; \r\n\r\n            G: string; \r\n\r\n            H: string; \r\n\r\n            I: string; \r\n\r\n            J: string; \r\n\r\n            K: string; \r\n\r\n        }); \r\n\r\n    }); \r\n\r\n    console.log(result); \r\n\r\n    return result; \r\n\r\n}<\/pre>\n<p>Learn more about Office Scripts, find resources, and watch video tutorials on the <a href=\"https:\/\/developer.microsoft.com\/office-scripts\">Office Scripts Developer Center<\/a>.<\/p>\n<p>Stay up to date, read the <a href=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/category\/office-scripts\/\">Office Scripts developer blog<\/a>.<\/p>\n<h3>About Uniper<\/h3>\n<p>Uniper is an international energy company with around 12,000 employees in more than 40 countries. The company plans to make its power generation CO2-neutral in Europe by 2035. For more information about Uniper visit <a href=\"https:\/\/www.uniper.energy\/\">https:\/\/www.uniper.energy\/<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using Office Scripts and Power Automate, I built a script that fully automates a report that used to take me at least one to two hours per month. <\/p>\n","protected":false},"author":69076,"featured_media":25159,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[11,20],"tags":[18,177],"class_list":["post-6522","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-office-add-ins","category-power-platform","tag-excel","tag-office-scripts"],"acf":[],"blog_post_summary":"<p>Using Office Scripts and Power Automate, I built a script that fully automates a report that used to take me at least one to two hours per month. <\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts\/6522","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/users\/69076"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/comments?post=6522"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts\/6522\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/media\/25159"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/media?parent=6522"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/categories?post=6522"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/tags?post=6522"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}