mvan231

Google Sheet Display Widget

Aug 22nd, 2021 (edited)
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. // The URL of your JSON endpoint
  2. const spreadsheetId = ""
  3.  
  4.  
  5. const endpoint = `https://docs.google.com/spreadsheets/d/${spreadsheetId}/gviz/tq?tqx=out:json`
  6.  
  7. // Function that performs the request to the JSON endpoint
  8. async function loadItems() {
  9. let at = endpoint
  10. let req = new Request(at)
  11. let corpo = await req.loadString()
  12. //log(corpo)
  13. let json = JSON.parse(corpo.substr(47).slice(0, -2))
  14.  
  15. log(json)
  16. // We return just the cells
  17. return json
  18. }
  19. // Request the spreadsheet data
  20. let json = await loadItems()
  21.  
  22. // Obtaining the content of the exact cell we are looking for
  23. StartBalance = json[4].content["$t"]
  24. CurrentBalance = json[5].content["$t"]
  25. SavingsPercent = json[6].content["$t"]
  26. SavingsAmount = json[7].content["$t"]
  27.  
  28. // Create the widget
  29. let w = new ListWidget()
  30. let fm = FileManager.iCloud();
  31. let path = fm.documentsDirectory() + "/hsbcbg.png";
  32. w.backgroundImage = fm.readImage(path);
  33.  
  34. // Add the start balance title to the widget
  35. w.addSpacer(6)
  36. t = w.addText("Banking")
  37. t.textColor = Color.white()
  38. t.font = new Font("San-Fransisco",20)
  39. w.addSpacer(-30)
  40. t = w.addText("__________________________________")
  41. w.addSpacer(13)
  42. t.textColor = Color.white()
  43. t.font = new Font("San-Fransisco",15)
  44. mainStack = w.addStack()
  45. leftStack = mainStack.addStack()
  46. mainStack.addSpacer()
  47. rightStack= mainStack.addStack()
  48. mainStack.layoutHorizontally()
  49. leftStack.layoutVertically()
  50. rightStack.layoutVertically()
  51.  
  52. // Add the start balance title to the widget
  53. t = leftStack.addText("Start Balance")
  54. t.textColor = Color.white()
  55. t.font = new Font("San-Fransisco",15)
  56.  
  57. // Add the start balance to the widget
  58. t = leftStack.addText(StartBalance)
  59. t.textColor = new Color("#4CD964")
  60. t.font = new Font("San-Fransisco",13)
  61.  
  62. leftStack.addSpacer(8)
  63.  
  64. // Add the current balance title to the widget
  65. t = leftStack.addText("Current Balance")
  66. t.textColor = Color.white()
  67. t.font = new Font("San-Fransisco",15)
  68.  
  69. // Add the current balance to the widget
  70. t = leftStack.addText(CurrentBalance)
  71. t.textColor = new Color("#4CD964")
  72. t.font = new Font("San-Fransisco",13)
  73.  
  74. // Add the current savings percent title to the widget
  75. t = rightStack.addText("% Saved")
  76. t.rightAlignText()
  77. t.textColor = Color.white()
  78. t.font = new Font("San-Fransisco",15)
  79.  
  80. // Add the current savings percent to the widget
  81. t = rightStack.addText(SavingsPercent)
  82. t.rightAlignText()
  83. //t.textColor = new Color("#4CD964")
  84. //t.font = new Font("San-Fransisco",13)
  85.  
  86. rightStack.addSpacer(8)
  87.  
  88. // Add the current savings title to the widget
  89. t = rightStack.addText("£ Saved")
  90. t.rightAlignText()
  91. let string = SavingsPercent
  92. let reg = /\-/
  93. let negative = reg.test(string)
  94. if (negative){
  95. t.textColor = new Color("#ff0000")
  96. }else{
  97. t.textColor = new Color("#4CD964")
  98. }
  99. t.font = new Font("San-Fransisco",13)
  100.  
  101. //t.textColor = Color.white()
  102. //t.font = new Font("San-Fransisco",15)
  103.  
  104. // Add the current savings to the widget
  105. //t = rightStack.addText(SavingsAmount)
  106. //t.rightAlignText()
  107. //t.textColor = new Color("#4CD964")
  108. //t.font = new Font("San-Fransisco",13)
  109.  
  110. // Add the current savings to the widget_
  111.  
  112. t = rightStack.addText(SavingsAmount)
  113. t.rightAlignText()
  114. negative = reg.test(SavingsAmount)
  115. if (negative){
  116. t.textColor = new Color("#ff0000")
  117. }else{
  118. t.textColor = new Color("#4CD964")
  119. }
  120. t.font = new Font("San-Fransisco",13)
  121.  
  122. // Position bottom text
  123. w.addSpacer()
  124.  
  125. w.presentMedium()
Add Comment
Please, Sign In to add comment