فهرست منبع

Harden Sheets names and Slides default body

Sheets: defaultBody now goes through normalizeBody, and defined-name access is routed through a new definedNames() helper that tolerates a missing or non-array names list. nameEntry skips empty or malformed entries, and the rename and Names dialog code no longer throws on such bodies. The dialog also edits and deletes by entry rather than by stale index.

Slides: the default deck is passed through normalizeBody so state such as embedded fonts or missing fields does not carry over from the previous deck.
Toby Chui 2 هفته پیش
والد
کامیت
2fea8cc941
2فایلهای تغییر یافته به همراه26 افزوده شده و 10 حذف شده
  1. 23 9
      src/web/Office/sheets/sheets.js
  2. 3 1
      src/web/Office/slides/slides.js

+ 23 - 9
src/web/Office/sheets/sheets.js

@@ -116,7 +116,7 @@ var SheetsApp = (function () {
         };
         };
     }
     }
     function defaultBody() {
     function defaultBody() {
-        return { sheets: [newSheetData("Sheet1")], active: 0 };
+        return normalizeBody({ sheets: [newSheetData("Sheet1")], active: 0, names: [] });
     }
     }
     function normalizeBody(b) {
     function normalizeBody(b) {
         if (!b || typeof b !== "object") b = {};
         if (!b || typeof b !== "object") b = {};
@@ -162,10 +162,20 @@ var SheetsApp = (function () {
             name.toUpperCase() !== "TRUE" && name.toUpperCase() !== "FALSE" &&
             name.toUpperCase() !== "TRUE" && name.toUpperCase() !== "FALSE" &&
             !F.lookupFunction(name);
             !F.lookupFunction(name);
     }
     }
+    /* The workbook's defined names; a body that skipped normalizeBody
+       (or has none) answers an empty list instead of throwing. */
+    function definedNames() {
+        if (!body) return [];
+        if (!Array.isArray(body.names)) body.names = [];
+        return body.names;
+    }
     function nameEntry(name) {
     function nameEntry(name) {
+        if (name === undefined || name === null || name === "") return null;
         var want = String(name).toUpperCase();
         var want = String(name).toUpperCase();
-        for (var i = 0; i < body.names.length; i++) {
-            if (body.names[i].name.toUpperCase() === want) return body.names[i];
+        var list = definedNames();
+        for (var i = 0; i < list.length; i++) {
+            var n = list[i];
+            if (n && n.name && String(n.name).toUpperCase() === want) return n;
         }
         }
         return null;
         return null;
     }
     }
@@ -1027,7 +1037,9 @@ var SheetsApp = (function () {
             // formulas (and defined names) that name the sheet follow the rename
             // formulas (and defined names) that name the sheet follow the rename
             if (v !== old) {
             if (v !== old) {
                 eachFormulaCell(function (cell) { cell.v = F.renameSheetRefs(cell.v, old, v); });
                 eachFormulaCell(function (cell) { cell.v = F.renameSheetRefs(cell.v, old, v); });
-                body.names.forEach(function (n) { n.formula = F.renameSheetRefs(n.formula, old, v); });
+                definedNames().forEach(function (n) {
+                    if (n && n.formula) n.formula = F.renameSheetRefs(n.formula, old, v);
+                });
             }
             }
             commit();
             commit();
             renderTabs();
             renderTabs();
@@ -2616,25 +2628,27 @@ var SheetsApp = (function () {
             '<div class="of-dim" id="shNameMsg"></div></div>');
             '<div class="of-dim" id="shNameMsg"></div></div>');
         function refresh() {
         function refresh() {
             var $t = $b.find(".sh-name-list tbody").empty();
             var $t = $b.find(".sh-name-list tbody").empty();
-            if (!body.names.length) {
+            var names = definedNames();
+            if (!names.length) {
                 $t.append('<tr><td colspan="3" class="of-dim">No names yet. ' +
                 $t.append('<tr><td colspan="3" class="of-dim">No names yet. ' +
                     'Select a range first to fill the box below with it.</td></tr>');
                     'Select a range first to fill the box below with it.</td></tr>');
             }
             }
-            body.names.forEach(function (n, i) {
+            names.forEach(function (n, i) {
                 var $tr = $("<tr></tr>");
                 var $tr = $("<tr></tr>");
                 $tr.append($("<td></td>").text(n.name));
                 $tr.append($("<td></td>").text(n.name));
                 var $ref = $('<input spellcheck="false">').val(n.formula);
                 var $ref = $('<input spellcheck="false">').val(n.formula);
                 $ref.on("change", function () {
                 $ref.on("change", function () {
                     var v = String($ref.val()).trim();
                     var v = String($ref.val()).trim();
                     if (!v) return;
                     if (!v) return;
-                    body.names[i].formula = v.charAt(0) === "=" ? v : "=" + v;
+                    n.formula = v.charAt(0) === "=" ? v : "=" + v;
                     commit();
                     commit();
                     renderAll();
                     renderAll();
                 });
                 });
                 $tr.append($("<td></td>").append($ref));
                 $tr.append($("<td></td>").append($ref));
                 var $del = $('<button class="of-btn" title="Delete"><i class="trash alternate outline icon"></i></button>');
                 var $del = $('<button class="of-btn" title="Delete"><i class="trash alternate outline icon"></i></button>');
                 $del.on("click", function () {
                 $del.on("click", function () {
-                    body.names.splice(i, 1);
+                    var at = names.indexOf(n);
+                    if (at >= 0) names.splice(at, 1);
                     commit();
                     commit();
                     renderAll();
                     renderAll();
                     refresh();
                     refresh();
@@ -2662,7 +2676,7 @@ var SheetsApp = (function () {
                 msg.text("Enter the range or value the name stands for.");
                 msg.text("Enter the range or value the name stands for.");
                 return;
                 return;
             }
             }
-            body.names.push({ name: name, formula: ref.charAt(0) === "=" ? ref : "=" + ref });
+            definedNames().push({ name: name, formula: ref.charAt(0) === "=" ? ref : "=" + ref });
             msg.text("");
             msg.text("");
             $b.find("#shNameNew").val("");
             $b.find("#shNameNew").val("");
             commit();
             commit();

+ 3 - 1
src/web/Office/slides/slides.js

@@ -295,7 +295,9 @@ var SlidesApp = (function () {
         var b = { size: [SLIDE_W, SLIDE_H], theme: "clean", slides: [] };
         var b = { size: [SLIDE_W, SLIDE_H], theme: "clean", slides: [] };
         body = b; // themeOf() needs it while building the first slide
         body = b; // themeOf() needs it while building the first slide
         b.slides.push(newSlide("title"));
         b.slides.push(newSlide("title"));
-        return b;
+        // through the same checks as an opened deck, so nothing (embedded
+        // fonts, missing fields) carries over from the previous one
+        return normalizeBody(b);
     }
     }
     /* A deck states its own size: 960 wide and as tall as its shape (a
     /* A deck states its own size: 960 wide and as tall as its shape (a
        4:3 PowerPoint deck is 960x720). Anything odd falls back to 16:9. */
        4:3 PowerPoint deck is 960x720). Anything odd falls back to 16:9. */