'[object Object] is not valid JSON' - even with basic script lab example snippet
Bug Report
All scripts are causing endless errors in the console when loading - even the base example.
Environment
- Host: Word (office online)
- OS: macOS
- Browser: Edge (latest version)
Expected behavior
Run the basic script lab example without issues.
Actual behavior
Note: I only (currently) see this on office.com/word, and not Word Desktop to MacOS.
Steps to Reproduce
- Use the simplest script lab example
- Run it
- See endless stream of the json error (see screenshot)
Screenshot
See above
Me too. Using the web version of Word. I also experimented with the desktop but problems there too but the failures are silent.
[ERROR]: Error:
{
"stack": "SyntaxError: Unexpected token 's', \"setImmedia\"... is not valid JSON\n at JSON.parse (<anonymous>)\n at n (https://script-lab.public.cdn.office.net/script-lab/7dttl/static/js/main.7a855f56.js:2:5403649)\n at undefined (https://script-lab.public.cdn.office.net/script-lab/7dttl/static/js/main.7a855f56.js:2:5403919)\n at Rh.onWindowMessage (https://script-lab.public.cdn.office.net/script-lab/7dttl/static/js/main.7a855f56.js:2:5404223)",
"message": "Unexpected token 's', \"setImmedia\"... is not valid JSON"
}
[ERROR]: Error:
{
"stack": "SyntaxError: Unexpected token 's', \"setImmedia\"... is not valid JSON\n at JSON.parse (<anonymous>)\n at n (https://script-lab.public.cdn.office.net/script-lab/7dttl/static/js/main.7a855f56.js:2:5403649)\n at undefined (https://script-lab.public.cdn.office.net/script-lab/7dttl/static/js/main.7a855f56.js:2:5403919)\n at Rh.onWindowMessage (https://script-lab.public.cdn.office.net/script-lab/7dttl/static/js/main.7a855f56.js:2:5404223)",
"message": "Unexpected token 's', \"setImmedia\"... is not valid JSON"
}
The code
// Import Word API
async function run() {
await Word.run(async (context) => {
// Get all paragraphs in the document
const paragraphs = context.document.body.paragraphs;
paragraphs.load("text");
await context.sync();
// Process in batches of, say, 50 paragraphs at a time
let batchSize = 50;
let totalParagraphs = paragraphs.items.length;
for (let i = 0; i < totalParagraphs; i += batchSize) {
let end = Math.min(i + batchSize, totalParagraphs);
let batch = paragraphs.items.slice(i, end);
let htmlContent = "<html><body>";
batch.forEach((paragraph, index) => {
if (index > 4)
return;
console.log(`Processing paragraph ${i + index + 1}`);
htmlContent += `<p>${paragraph.text}</p>`;
});
htmlContent += "</body></html>";
// Display the current batch's result or save it in some way
console.log(`Processed batch ${i + 1} to ${end}`);
}
});
}
// Attach the run function to the ribbon or button click
(async () => {
try {
await run();
} catch (error: any) {
console.error('Error:', error);
}
})();
I did a bit of digging and I think the problem is that the scriptlab code assumes all browser message events have the same format. This bit of the code assumes all message events have 'event.data' as a string, which isn't gonna be the case with anything else on the page that is sending messages.
Thanks. This is why I make a lot of use of "?. ".
When I run on the desktop, I don't see the errors. It just fails silently. At least, I assume that's why I don't see the message and don't see the download button.
Closing all issues since this repo is being archived and no longer maintained.