sheet-protection-xform.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. const _ = require('../../../utils/under-dash');
  2. const BaseXform = require('../base-xform');
  3. function booleanToXml(model, value) {
  4. return model ? value : undefined;
  5. }
  6. function xmlToBoolean(value, equals) {
  7. return value === equals ? true : undefined;
  8. }
  9. class SheetProtectionXform extends BaseXform {
  10. get tag() {
  11. return 'sheetProtection';
  12. }
  13. render(xmlStream, model) {
  14. if (model) {
  15. const attributes = {
  16. sheet: booleanToXml(model.sheet, '1'),
  17. selectLockedCells: model.selectLockedCells === false ? '1' : undefined,
  18. selectUnlockedCells: model.selectUnlockedCells === false ? '1' : undefined,
  19. formatCells: booleanToXml(model.formatCells, '0'),
  20. formatColumns: booleanToXml(model.formatColumns, '0'),
  21. formatRows: booleanToXml(model.formatRows, '0'),
  22. insertColumns: booleanToXml(model.insertColumns, '0'),
  23. insertRows: booleanToXml(model.insertRows, '0'),
  24. insertHyperlinks: booleanToXml(model.insertHyperlinks, '0'),
  25. deleteColumns: booleanToXml(model.deleteColumns, '0'),
  26. deleteRows: booleanToXml(model.deleteRows, '0'),
  27. sort: booleanToXml(model.sort, '0'),
  28. autoFilter: booleanToXml(model.autoFilter, '0'),
  29. pivotTables: booleanToXml(model.pivotTables, '0'),
  30. };
  31. if (model.sheet) {
  32. attributes.algorithmName = model.algorithmName;
  33. attributes.hashValue = model.hashValue;
  34. attributes.saltValue = model.saltValue;
  35. attributes.spinCount = model.spinCount;
  36. attributes.objects = booleanToXml(model.objects === false, '1');
  37. attributes.scenarios = booleanToXml(model.scenarios === false, '1');
  38. }
  39. if (_.some(attributes, value => value !== undefined)) {
  40. xmlStream.leafNode(this.tag, attributes);
  41. }
  42. }
  43. }
  44. parseOpen(node) {
  45. switch (node.name) {
  46. case this.tag:
  47. this.model = {
  48. sheet: xmlToBoolean(node.attributes.sheet, '1'),
  49. objects: node.attributes.objects === '1' ? false : undefined,
  50. scenarios: node.attributes.scenarios === '1' ? false : undefined,
  51. selectLockedCells: node.attributes.selectLockedCells === '1' ? false : undefined,
  52. selectUnlockedCells: node.attributes.selectUnlockedCells === '1' ? false : undefined,
  53. formatCells: xmlToBoolean(node.attributes.formatCells, '0'),
  54. formatColumns: xmlToBoolean(node.attributes.formatColumns, '0'),
  55. formatRows: xmlToBoolean(node.attributes.formatRows, '0'),
  56. insertColumns: xmlToBoolean(node.attributes.insertColumns, '0'),
  57. insertRows: xmlToBoolean(node.attributes.insertRows, '0'),
  58. insertHyperlinks: xmlToBoolean(node.attributes.insertHyperlinks, '0'),
  59. deleteColumns: xmlToBoolean(node.attributes.deleteColumns, '0'),
  60. deleteRows: xmlToBoolean(node.attributes.deleteRows, '0'),
  61. sort: xmlToBoolean(node.attributes.sort, '0'),
  62. autoFilter: xmlToBoolean(node.attributes.autoFilter, '0'),
  63. pivotTables: xmlToBoolean(node.attributes.pivotTables, '0'),
  64. };
  65. if (node.attributes.algorithmName) {
  66. this.model.algorithmName = node.attributes.algorithmName;
  67. this.model.hashValue = node.attributes.hashValue;
  68. this.model.saltValue = node.attributes.saltValue;
  69. this.model.spinCount = parseInt(node.attributes.spinCount, 10);
  70. }
  71. return true;
  72. default:
  73. return false;
  74. }
  75. }
  76. parseText() {}
  77. parseClose() {
  78. return false;
  79. }
  80. }
  81. module.exports = SheetProtectionXform;