|
1 | 1 | using System;
|
| 2 | +using System.Collections.Generic; |
2 | 3 | using System.Net;
|
3 | 4 | using System.Net.Http;
|
4 | 5 | using System.Text;
|
@@ -70,7 +71,7 @@ public static async Task<object> ImportHtml(
|
70 | 71 | string url,
|
71 | 72 | [ExcelArgument(Description = "Type of data to import. Accepts either 'table' for HTML tables or 'list' for HTML lists (ul/ol).")]
|
72 | 73 | string dataType,
|
73 |
| - [ExcelArgument(Description = "Zero-based index of the table or list to import from the HTML page. For example, 0 for the first table/list, 1 for the second, and so on.")] |
| 74 | + [ExcelArgument(Description = "One-based index of the table or list to import from the HTML page. For example, 1 for the first table/list, 2 for the second, and so on.")] |
74 | 75 | int index)
|
75 | 76 | {
|
76 | 77 | if (string.IsNullOrWhiteSpace(url))
|
@@ -106,43 +107,86 @@ public static async Task<object> ImportHtml(
|
106 | 107 | }
|
107 | 108 | }
|
108 | 109 |
|
109 |
| - static object ExtractTable(HtmlDocument doc, int index) |
| 110 | + [ExcelFunction(Description = "Imports data from a given URL")] |
| 111 | + public static async Task<object> HttpGet(string url) |
| 112 | + { |
| 113 | + if (string.IsNullOrWhiteSpace(url)) |
| 114 | + { |
| 115 | + return "Error: URL is required"; |
| 116 | + // return ExcelError.ExcelErrorValue; |
| 117 | + } |
| 118 | + |
| 119 | + try |
| 120 | + { |
| 121 | + var response = await _httpClient.GetStringAsync(url); |
| 122 | + return response; |
| 123 | + } |
| 124 | + catch (HttpRequestException rex) |
| 125 | + { |
| 126 | + return $"Error: Unable to fetch data from the URL - {rex.Message}"; |
| 127 | + } |
| 128 | + catch (Exception ex) |
| 129 | + { |
| 130 | + return $"Error: {ex.Message}"; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + static object ExtractTable(HtmlDocument doc, int indexOneBased) |
110 | 135 | {
|
111 | 136 | var tables = doc.DocumentNode.SelectNodes("//table");
|
112 |
| - if (tables == null || tables.Count <= index) |
| 137 | + if (tables == null || tables.Count < indexOneBased) |
113 | 138 | return "Error: Table not found";
|
114 | 139 |
|
115 |
| - var table = tables[index]; |
116 |
| - var sb = new StringBuilder(); |
| 140 | + var table = tables[indexOneBased - 1]; |
117 | 141 |
|
118 |
| - foreach (var row in table.SelectNodes("tr")) |
| 142 | + var results = new List<List<string>>(); |
| 143 | + foreach (var row in table.SelectNodes(".//tr")) |
119 | 144 | {
|
120 |
| - foreach (var cell in row.SelectNodes("th|td")) |
| 145 | + var rowResult = new List<string>(); |
| 146 | + foreach (var cell in row.SelectNodes(".//th|.//td")) |
121 | 147 | {
|
122 |
| - sb.Append(cell.InnerText.Trim()); |
123 |
| - sb.Append("\t"); // Tab-separated values |
| 148 | + rowResult.Add(cell.InnerText.Trim()); |
124 | 149 | }
|
125 |
| - sb.AppendLine(); // New line at the end of each row |
| 150 | + results.Add(rowResult); |
126 | 151 | }
|
127 | 152 |
|
128 |
| - return sb.ToString(); |
| 153 | + if (results.Count == 0 || results[0].Count == 0) |
| 154 | + return "Error: No data found in the table"; |
| 155 | + |
| 156 | + // Convert results to a 2D object array |
| 157 | + var resultArray = new object[results.Count, results[0].Count]; |
| 158 | + for (int i = 0; i < results.Count; i++) |
| 159 | + { |
| 160 | + for (int j = 0; j < results[i].Count; j++) |
| 161 | + { |
| 162 | + resultArray[i, j] = results[i][j]; |
| 163 | + } |
| 164 | + } |
| 165 | + return results; |
129 | 166 | }
|
130 | 167 |
|
131 |
| - static object ExtractList(HtmlDocument doc, int index) |
| 168 | + static object ExtractList(HtmlDocument doc, int indexOneBased) |
132 | 169 | {
|
133 | 170 | var lists = doc.DocumentNode.SelectNodes("//ul | //ol");
|
134 |
| - if (lists == null || lists.Count <= index) |
| 171 | + if (lists == null || lists.Count < indexOneBased) |
135 | 172 | return "Error: List not found";
|
136 | 173 |
|
137 |
| - var list = lists[index]; |
138 |
| - var sb = new StringBuilder(); |
| 174 | + var list = lists[indexOneBased-1]; |
| 175 | + |
| 176 | + var results = new List<string>(); |
| 177 | + foreach (var item in list.SelectNodes(".//li")) |
| 178 | + { |
| 179 | + results.Add(item.InnerText.Trim()); |
| 180 | + } |
139 | 181 |
|
140 |
| - foreach (var item in list.SelectNodes("li")) |
| 182 | + // Convert results to a 2D object array with a single column |
| 183 | + var resultArray = new object[results.Count, 1]; |
| 184 | + for (int i = 0; i < results.Count; i++) |
141 | 185 | {
|
142 |
| - sb.AppendLine(item.InnerText.Trim()); |
| 186 | + resultArray[i, 0] = results[i]; |
143 | 187 | }
|
144 | 188 |
|
145 |
| - return sb.ToString(); |
| 189 | + return results; |
146 | 190 | }
|
147 | 191 | }
|
148 | 192 | }
|
0 commit comments