Guest User

Untitled

a guest
Nov 22nd, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. import * as React from "react"
  2. import * as OfficeHelpers from "@microsoft/office-js-helpers"
  3.  
  4. const tableName = `tableName`;
  5. const tableRange = "B1:E1";
  6. const excelHeaders = ["Firstname", "Lastname", "Age", "City"];
  7. const excelData = [["Mario", "Rossi", 40, "Milano"], ["Mario", "Verdi", 60, "Roma"], ["Gianni", "Neri", 50, "Catania"], ["Gianni", "Bianchi", 80, "Palermo"]];
  8.  
  9. class App extends React.Component {
  10. constructor(props, context) {
  11. super(props, context)
  12.  
  13. this.state = {
  14. slice: 0,
  15. }
  16. }
  17.  
  18. useAddRows = (slice: number = excelData.length) => {
  19. Excel.run(context => {
  20. const _excelData = excelData.slice(0, slice)
  21.  
  22. const currentWorksheet = context.workbook.worksheets.getItemOrNullObject(
  23. "Sheet1"
  24. )
  25. let excelTable = currentWorksheet.tables.getItemOrNullObject(tableName)
  26.  
  27. excelTable.rows.load("values")
  28. excelTable.columns.load("values")
  29.  
  30. return context.sync().then(() => {
  31. if (excelTable.isNullObject) {
  32. excelTable = currentWorksheet.tables.add(tableRange, true)
  33. excelTable.name = tableName
  34. excelTable.getHeaderRowRange().values = [excelHeaders]
  35. excelTable.rows.add(null, _excelData)
  36. } else {
  37. const tableRows = excelTable.rows.items.length
  38. const tableColumns = excelTable.columns.items.length
  39. const dataRows = _excelData.length
  40. const dataColumns = _excelData[0].length
  41.  
  42. for (let i = tableRows - 1; i >= 0; i--) {
  43. // Deletes all table rows
  44. excelTable.rows.items[i].delete()
  45. }
  46.  
  47. excelTable.rows.add(0, _excelData)
  48. }
  49.  
  50. excelTable.getRange().format.autofitColumns()
  51. excelTable.getRange().format.autofitRows()
  52.  
  53. return context.sync()
  54. })
  55. }).catch(function(error) {
  56. Debug.error(
  57. `ErrorCode: ${error.code}`,
  58. `ErrorMessage: ${error.message}`,
  59. `ErrorDebugInfo: ${error.debugInfo}`
  60. )
  61.  
  62. if (error instanceof OfficeExtension.Error) {
  63. Debug.error("Debug info: " + JSON.stringify(error.debugInfo))
  64. }
  65. })
  66. }
  67.  
  68. render() {
  69. const { slice } = this.state
  70.  
  71. const styl = {
  72. dataSizeBtn: {
  73. padding: "2px 16px",
  74. },
  75. rowButtonsContainer: {
  76. display: "flex",
  77. justifyContent: "space-around",
  78. },
  79. }
  80.  
  81. return (
  82. <div className="taskpane" style={{ paddingTop: "10px" }}>
  83. <div style={styl.rowButtonsContainer}>
  84. <button
  85. style={styl.dataSizeBtn}
  86. onClick={() => {
  87. this.useAddRows(1)
  88. }}
  89. >
  90. 1
  91. </button>
  92. <button
  93. style={styl.dataSizeBtn}
  94. onClick={() => {
  95. this.useAddRows(2)
  96. }}
  97. >
  98. 2
  99. </button>
  100. <button
  101. style={styl.dataSizeBtn}
  102. onClick={() => {
  103. this.useAddRows(3)
  104. }}
  105. >
  106. 3
  107. </button>
  108. <button
  109. style={styl.dataSizeBtn}
  110. onClick={() => {
  111. this.useAddRows(4)
  112. }}
  113. >
  114. 4
  115. </button>
  116. <button
  117. style={styl.dataSizeBtn}
  118. onClick={() => {
  119. const resultSet = Math.floor((Math.random() * 1000) % 5)
  120. Debug.print(`resultSet: ${resultSet}`)
  121.  
  122. this.useAddRows(resultSet)
  123. }}
  124. >
  125. RAND
  126. </button>
  127. </div>
  128. </div>
  129. )
  130. }
  131. }
  132. }
Add Comment
Please, Sign In to add comment