Advertisement
Fsteff

Big Excel Formula

Dec 21st, 2024 (edited)
156
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.07 KB | Source Code | 0 0
  1. =LET(
  2. Range; B23:Z3540;
  3. Header; "#define REGISTER_LIST {";
  4. MinSeperation; 3;
  5.  
  6. Note; "Read this forumla buttom-up, as a c-program.";
  7.  
  8. IsolateColumn; LAMBDA(cell;col_letter; LET(
  9. Usage; "(A7, 'Z') returns Z7";
  10. rows; ROW(cell);
  11. new_range; col_letter & MIN(rows) & ":" & col_letter & MAX(rows);
  12. INDIRECT(new_range)
  13. ));
  14.  
  15. IsolateColumns; LAMBDA(range;col_letters; LET(
  16. Usage; "(A7:D20, D:G) returns D7:G20";
  17. rows; ROW(range);
  18. cols; TEXTSPLIT(col_letters; ":");
  19. start_col; INDEX(cols; 1);
  20. end_col; INDEX(cols; 2);
  21. new_range; start_col & MIN(rows) & ":" & end_col & MAX(rows);
  22. INDIRECT(new_range)
  23. ));
  24.  
  25. Indent1; REPT(" "; MinSeperation);
  26.  
  27. checks2bin_Desc; "Range to a binary string. All x or X is 1, otherwise it's 0";
  28. checks2bin; LAMBDA(range; TEXTJOIN(""; TRUE; IF(ISTEXT(range); IF(UPPER(range)="X"; "1"; "0"); "0")));
  29.  
  30. bin16_2hex_Desc; "16-bit string to 4 cifer hex string";
  31. bin16_2hex; LAMBDA(binStr; BIN2HEX(LEFT(binStr;8);2)&BIN2HEX(RIGHT(binStr;8);2));
  32.  
  33. FixStringLength_Desc; "Truncates or pads a string to a fixed length.";
  34. FixStringLength; LAMBDA(str;length; LEFT(str & REPT(" "; length); length));
  35.  
  36. RegAttributeValues; LAMBDA(row; HEX2DEC(bin16_2hex(checks2bin(INDEX(RegAttributes; row; 0)))));
  37.  
  38. GetStringMaxLength; LAMBDA(range; MAX(LEN(range)));
  39.  
  40. RegAddress; IsolateColumn(Range; "B");
  41. RegCategory; IsolateColumn(Range; "D");
  42. RegName; IsolateColumn(Range; "E");
  43. RegDescription; IsolateColumn(Range; "F");
  44. RegType; IsolateColumn(Range; "G");
  45. RegFormatType; IsolateColumns(Range; "H:H");
  46. RegValue; IsolateColumn(Range; "I");
  47. RegAttributes; IsolateColumns(Range; "J:Y");
  48. RegAddressMaxLength; MinSeperation + LEN("0x0000");
  49. RegCategoryMaxLength; MinSeperation + GetStringMaxLength(RegCategory) +2;
  50. RegNameMaxLength; MinSeperation + LEN("X_BITFIELD(") + GetStringMaxLength(RegName)*2;
  51. RegTypeMaxLength; MinSeperation + GetStringMaxLength(RegType);
  52. RegValueMaxLength; RegAddressMaxLength + RegTypeMaxLength + RegAddressMaxLength*2;
  53. RegAttributeLength; RegAddressMaxLength;
  54.  
  55. FindLast; LAMBDA(row;search_string; LET(
  56. Usage; "Return the last value of a column scanning upwards";
  57. search_upper; UPPER(search_string);
  58. rows_to_check; SEQUENCE(row; 1; row; -1);
  59. matching_row; XLOOKUP(search_upper; UPPER(INDEX(RegFormatType; rows_to_check)); rows_to_check; "");
  60. IF(matching_row = ""; ""; INDEX(RegName; matching_row))
  61. ));
  62.  
  63. ToCString; LAMBDA(string0; LET(
  64. string1; SUBSTITUTE(string0; "\"; "\\");
  65. string2; SUBSTITUTE(string1; CHAR(13)&CHAR(10); "\n");
  66. string3; SUBSTITUTE(string2; CHAR(13); "\n");
  67. string4; SUBSTITUTE(string3; CHAR(10); "\n");
  68. string5; SUBSTITUTE(string4; CHAR(9); "\t");
  69. string6; SUBSTITUTE(string5; CHAR(34); "\""");
  70. string6
  71. ));
  72.  
  73. OutRegName; LAMBDA(row;prefix;
  74. FixStringLength(Indent & prefix & "(" & INDEX(RegName; row) & ","; RegNameMaxLength)
  75. );
  76.  
  77. OutRegAddress; LAMBDA(row;
  78. FixStringLength("0x" & DEC2HEX(INDEX(RegAddress; row); 4) & ","; RegAddressMaxLength)
  79. );
  80.  
  81. OutRegType; LAMBDA(row;
  82. FixStringLength(INDEX(RegType; row) & ","; RegTypeMaxLength)
  83. );
  84.  
  85. OutCategory; LAMBDA(row;
  86. FixStringLength("""" & INDEX(RegCategory; row) & """" & ","; RegCategoryMaxLength)
  87. );
  88.  
  89. OutRegValue; LAMBDA(row;
  90. FixStringLength("0x" & DEC2HEX(RegAttributeValues(row); 4) & ","; RegAttributeLength)
  91. );
  92.  
  93. OutDescription; LAMBDA(row;
  94. """" & ToCString(INDEX(RegDescription; row)) & """"
  95. );
  96.  
  97. OutDetailName; LAMBDA(row;prefix;
  98. FixStringLength(Indent & Indent & prefix & "(" & FindLast(row; IF(UPPER(prefix)="X_ITEM"; "List"; "Bitfield")) & "_" & INDEX(RegName; row) & ","; RegNameMaxLength)
  99. );
  100.  
  101. OutDetailValue; LAMBDA(row;
  102. FixStringLength(INDEX(RegValue; row) & ","; RegValueMaxLength)
  103. );
  104.  
  105. Finalize; LAMBDA(row;type; LET(
  106. next_format; IF(row < ROWS(RegFormatType); INDEX(RegFormatType; row+1); "");
  107. IF(UPPER(next_format) <> UPPER(type); ") )"; ")")
  108. ));
  109.  
  110. Format_Data; LAMBDA(row;
  111. OutRegName(row; "X") &
  112. OutRegAddress(row) &
  113. OutRegType(row) &
  114. OutRegValue(row) &
  115. OutCategory(row) &
  116. OutDescription(row) & " )"
  117. );
  118.  
  119. Format_List; LAMBDA(row;
  120. OutRegName(row; "X_LIST") &
  121. OutRegAddress(row) &
  122. OutRegType(row) &
  123. OutRegValue(row) &
  124. OutCategory(row) &
  125. OutDescription(row) & ","
  126. );
  127.  
  128. Format_Item; LAMBDA(row;
  129. OutDetailName(row; "X_ITEM") &
  130. OutDetailValue(row) &
  131. OutDescription(row) & Finalize(row; "ITEM")
  132. );
  133.  
  134. Format_Bitfield; LAMBDA(row;
  135. OutRegName(row; "X_BITFIELD") &
  136. OutRegAddress(row) &
  137. OutRegType(row) &
  138. OutRegValue(row) &
  139. OutCategory(row) &
  140. OutDescription(row) & ","
  141. );
  142.  
  143. Format_Bit; LAMBDA(row;
  144. OutDetailName(row; "X_BIT") &
  145. OutDetailValue(row) &
  146. OutDescription(row) & Finalize(row; "BIT")
  147. );
  148.  
  149. Main; LAMBDA(row; LET(
  150. Usage; "Process a single row depending on it's format.";
  151. format; INDEX(RegFormatType; row);
  152. SWITCH( UPPER(format);
  153. "DATA"; Format_Data(row);
  154. "LIST"; Format_List(row);
  155. "ITEM"; Format_Item(row);
  156. "BITFIELD"; Format_Bitfield(row);
  157. "BIT"; Format_Bit(row);
  158. ""; " ";
  159. "Error: Unknown Type Detail"
  160. )));
  161.  
  162. TrimSpaces; LAMBDA(Range; LET(
  163. SpaceStrings; MAP(Range; LAMBDA(cell; INDEX(TEXTSPLIT(cell; ",";; TRUE); 2)));
  164. SpacesInStrings; MAP(SpaceStrings; LAMBDA(str; IFERROR(LEN(str) - LEN(SUBSTITUTE(str; " "; "")); "")));
  165. SpacesToRemove;MIN(SpacesInStrings) - MinSeperation;
  166. RemoveString;REPT(" ";SpacesToRemove);
  167. SUBSTITUTE(Range;RemoveString;"";1)
  168. ));
  169.  
  170. MainOutput_Desc; "Iterates the rows in Range, converts to lines in a c header file, intented for an x-macro. Adds header and ensures all lines are continued at the same char. Then adds footer. "
  171. MainOutput; MAP(SEQUENCE(ROWS(Range)); Main);
  172. TrimmedOutput; TrimSpaces(MainOutput);
  173. AddLineContinuation; MAP(VSTACK(Header; TrimmedOutput); LAMBDA(line; FixStringLength(line; GetStringMaxLength(TrimmedOutput)) & " \"));
  174. VSTACK(AddLineContinuation; " }")
  175. )
  176.  
  177.  
Tags: excel formula
Advertisement
Comments
  • Fsteff
    176 days
    # text 0.25 KB | 0 0
    1. This is an early version of the header export formula. Needs a bit of cleanup.
    2. Exceeds Excel's max 8192 bytes formula size.
    3.  
    4. It's recommended to edit the formula in Notepad++ or similar, which highlights selected words and add some syntax coloring.
Add Comment
Please, Sign In to add comment
Advertisement