我正在开发一个绑定到电子表格的 Google Apps 脚本项目。对于某些功能,我显示一个模式对话框,如下所示:模式对话框。因此,一系列数量可变的选择器根据某些数据动态创建和加载。每个选择器都包含在
合法参数和返回值是 JavaScript 原语,如 Number、Boolean、String 或 null,以及由原语、对象和数组组成的 JavaScript 对象和数组。页面内的表单元素作为参数也是合法的,但它必须是函数的唯一参数,并且作为返回值是不合法的。如果您尝试传递除表单之外的日期、函数、DOM 元素或其他禁止类型(包括对象或数组内的禁止类型),请求将失败。创建循环引用的对象也会失败,数组中未定义的字段将变为空。
这是整个 HTML 端函数 endDialog:
/**
* Calls a server-side function to apply the changes \
* to the categories' colours and closes the dialog.
*
* @return {void} Nothing.
*/
function endDialog() {
// Hide the error messages.
document.getElementById("emptyColour").style = "display:none;";
document.getElementById("repeatedColour").style = "display:none;";
// Get the list of categories.
const categories = Array.from(document.getElementById("categories").children);
// Create a map to contain the list of category-colour pairs.
const rowPairs = new Map();
// Create an array to contain the column of boolean values
// corresponding to the availabilities of each colour palette.
// Initialise all colours as being available.
const colourAvailabilities = Array.from({length:10}).map(() => [true]);
// A variable to see if all colours have been selected properly.
var validSelects = true;
// A variable to see if all colours are different from each other.
var uniqueSelects = true;
// Initialise an index variable to go through the categories.
var i=0;
// While the colours are validly selected,
// and unique, go through the categories.
while (validSelects && uniqueSelects && i<categories.length) {
// Get the corresponding row number,
let catRow = Number(categories[i].value);
// and its chosen colour's row number.
let colourRow = Number(document.getElementById(i+1)
.value
.split(',')[0]);
// Check if the selected option is a valid colour.
validSelects = colourRow != 0;
// If the selected option is a valid colour,
if (validSelects) {
// set the corresponding colour to being unavailable.
colourAvailabilities[colourRow-6][0] = false;
// Add the rows pair to the map.
// Use the colour row number as the key, so as to easily check whether the
// number of categories and the number of key-value pairs in the map are equal.
// Indeed, if this colour has already been used, no new key-value pair will
// be added, instead just updating the value associated to the colour's row key.
rowPairs.set(colourRow, catRow);
// Check if all selected colours are different from each other.
uniqueSelects = rowPairs.size == i+1;
}
// Go to the next category.
i++;
}
// If the user has made a valid and unique colour selection,
if (validSelects && uniqueSelects) {
console.log(rowPairs, colourAvailabilities);
// call a server-side function to apply the changes.
google.script.run.applyColouring(rowPairs, colourAvailabilities);
// Close the dialog.
google.script.host.close();
}
// If one of the selected colours is invalid,
else if (!validSelects) {
// display an error message.
document.getElementById("emptyColour").style = "display:block;";
}
// If some colour has been selected more than once,
else {
// display an error message.
document.getElementById("repeatedColour").style = "display:block;";
}
}
我认为我没有必要共享其余代码,但如果您想要/需要服务器端功能代码或整个 HTML 文件,我可以发布它们。
如果有人知道问题是什么,我洗耳恭听!预先感谢。
您无法通过
Map
接口发送 google.script.run
对象,因为地图无法序列化。
来自文档:
没有对序列化或解析的本机支持。(但是您可以通过使用
及其Map
replacer参数,以及使用JSON.stringify()
及其reviver参数来构建对JSON.parse()
的自己的序列化和解析支持。请参阅Stack Overflow 问题如何 JSON.stringify 一个 ES6 映射?)。
因此,要么使用普通的
Object
,要么使用您自己的 replacer和
reviver函数在发送端使用
Map
和接收端 JSON.stringify()
序列化 JSON.parse()
。
您无法通过
Map
接口发送 google.script.run
对象,因为地图无法序列化。
来自文档:
没有对序列化或解析的本机支持。(但是您可以通过使用
及其Map
replacer参数,以及使用JSON.stringify()
及其reviver参数来构建对JSON.parse()
的自己的序列化和解析支持。请参阅Stack Overflow 问题如何 JSON.stringify 一个 ES6 映射?)。
因此,要么使用普通的
Object
,要么使用您自己的 replacer和
reviver函数在发送端使用
Map
和接收端 JSON.stringify()
序列化 JSON.parse()
。