Last active
January 7, 2023 07:01
-
-
Save superyngo/5afdd9032ce99898697969a2908d3def to your computer and use it in GitHub Desktop.
UDF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: WenUDF | |
| description: UDF | |
| host: EXCEL | |
| api_set: {} | |
| script: | |
| content: | | |
| //20220503 | |
| /** | |
| * Get Range. | |
| * @customfunction | |
| * @param {Range} | {array} | |
| * @returns {any[][]} a range | |
| */ | |
| function tsRange(operands: any[][]) { | |
| let list: any[][] = operands; | |
| console.log(list); | |
| return list; | |
| } | |
| /** | |
| * Get Numeric from each cell. | |
| * @customfunction | |
| * @param {Range} | {array} | |
| * @returns {number[][]} A dynamic array with only numerics. | |
| */ | |
| function tsNumeric(refRange: any[][]) { | |
| let resultRange: any[][] = refRange; | |
| resultRange.forEach(function(row, row_i) { | |
| row.forEach(function(cell, cell_i) { | |
| resultRange[row_i][cell_i] = String(cell).replace(/\D/g, ""); | |
| }); | |
| }); | |
| return resultRange; | |
| } | |
| /** | |
| * MULTPLY2. | |
| * @customfunction | |
| * @param operands A number (such as 1 or 3.1415), a cell address (such as A1 or $E$11), or a range of cell addresses (such as B3:F12) | |
| * @return {number[][]} | |
| */ | |
| function MULTPLY2(operands: number[][]): number[][] { | |
| let result: number[][] = operands; | |
| operands.forEach((row, rowi) => { | |
| row.forEach((num, numi) => { | |
| result[rowi][numi] = num * 2; | |
| }); | |
| }); | |
| return result; | |
| } | |
| /** | |
| * CheckBlank. | |
| * @customfunction | |
| * @param operands Any value, a cell address (such as A1 or $E$11), or a range of cell addresses (such as B3:F12) | |
| * @param {any} Valur to repalce true | |
| * @param {any} Value to replace False | |
| * @return {any[][]} | |
| */ | |
| function IfBlankOrZero(operands: any[][], TrueRplace: any): any[][] { | |
| const result2 = operands.map((i) => i.map((j) => j === 0)); | |
| return result2; | |
| } | |
| /** | |
| * 將表格轉換為 JSON 輸出。 | |
| * @customfunction | |
| * @param range 目標範圍。 | |
| * @returns 表格的 JSON 輸出。 | |
| */ | |
| function tableToJson(range: any[][]): string { | |
| // 建立一個陣列,用來儲存結果 | |
| const result: any[] = []; | |
| // 取得表格的欄位名稱 | |
| const fieldNames: string[] = range[0]; | |
| // 對目標範圍的每個儲存格進行迴圈 | |
| for (let row = 1; row < range.length; row++) { | |
| // 建立一個物件,用來儲存這一列的資料 | |
| const obj: any = {}; | |
| for (let col = 0; col < range[row].length; col++) { | |
| // 將欄位名稱當作屬性名稱,儲存格值當作屬性值 | |
| obj[fieldNames[col]] = range[row][col]; | |
| } | |
| // 將物件加入結果陣列 | |
| result.push(obj); | |
| } | |
| // 將結果陣列轉換為 JSON 字串並回傳 | |
| return JSON.stringify(result); | |
| } | |
| /** | |
| * 將 JSON 轉換為二維陣列。 | |
| * @customfunction | |
| * @param json 要轉換的 JSON。 | |
| * @returns 轉換後的二維陣列。 | |
| */ | |
| function jsonToArray(jsonSource: any): any[][] { | |
| //console.log(jsonSource) | |
| console.log(JSON.parse(jsonSource)); | |
| const json = JSON.parse(jsonSource); | |
| // 建立一個陣列,用來儲存結果 | |
| const result: any[][] = []; | |
| // 取得第一個物件的屬性名稱,作為標題行 | |
| const fieldNames: string[] = Object.keys(json[0]); | |
| result.push(fieldNames); | |
| // 對 JSON 中的每個物件進行迴圈 | |
| for (const obj of json) { | |
| // 建立一個陣列,用來儲存這個物件的屬性值 | |
| const arr: any[] = []; | |
| // 對這個物件的屬性進行迴圈 | |
| for (const key in obj) { | |
| // 將屬性值轉換為字串,並加入陣列 | |
| arr.push(obj[key]); | |
| } | |
| // 將陣列加入結果陣列 | |
| result.push(arr); | |
| } | |
| // 回傳結果陣列 | |
| return result; | |
| } | |
| language: typescript | |
| libraries: | | |
| https://appsforoffice.microsoft.com/lib/1/hosted/office.js | |
| @types/office-js | |
| [email protected]/client/core.min.js |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment