将数组添加到 Google 表格的自定义 Google Apps 脚本
Adding Array to Custom Google Apps Script for Google Sheets
我有一些代码可以使用邮政编码查找县,我希望能够将该函数用作数组公式,以便我可以将它添加到 sheet 以进行表单响应.
我试图查找如何使该函数与数组公式一起使用,但我不太了解如何应用它。
是否可以将此代码用作数组公式?
样本sheet:
https://docs.google.com/spreadsheets/d/1S-TVchlE1sFTIOrbOL-v_N3LJJHLAHLJHkUtVpFeZok/edit#gid=0
/*
*
* Google Maps Formulas for Google Sheets
*
* Written by Amit Agarwal
*
* Web: https://www.labnol.org/google-maps-sheets-200817
*
*/
const md5 = (key = "") => {
const code = key.toLowerCase().replace(/\s/g, "");
return Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, key)
.map((char) => (char + 256).toString(16).slice(-2))
.join("");
};
const getCache = (key) => {
return CacheService.getDocumentCache().get(md5(key));
};
const setCache = (key, value) => {
const expirationInSeconds = 6 * 60 * 60; // max is 6 hours
CacheService.getDocumentCache().put(md5(key), value, expirationInSeconds);
};
/**
* Get the county name of an address on Google Maps.
*
* =GOOGLEMAPS_COUNTY("10 Hanover Square, NY")
*
* @param {String} address The address to lookup.
* @return {String} The county of the address.
* @customFunction
*/
const GOOGLEMAPS_COUNTY = (address) => {
if (!address) {
throw new Error("No address specified!");
}
if (address.map) {
return address.map("administrative_area_level_2");
}
const key = ["administrative_area_level_2", address].join(",");
const value = getCache(key);
if (value !== null) return value;
const { results: [data = null] = [] } = Maps.newGeocoder().geocode(address);
if (data === null) {
throw new Error("Address not found!");
}
const [{ long_name } = {}] = data.address_components.filter(
({ types: [level] }) => {
return level === "administrative_area_level_2";
}
);
if (!long_name) {
throw new Error("County not found!");
}
const answer = `${long_name}`;
setCache(key, answer);
return answer;
};
在您的情况下,以下示例脚本是您的预期目标吗?
示例脚本:
请将以下脚本添加到您当前的脚本中并保存。
const GOOGLEMAPS_COUNTY_ARRAY = values => values.map(r => r.map(c => c.toString() ? GOOGLEMAPS_COUNTY(c) : null));
使用本脚本时,请将自定义函数=GOOGLEMAPS_COUNTY_ARRAY(A2:A)
放入单元格。
测试:
使用您的示例电子表格时,将获得以下结果。
参考:
我有一些代码可以使用邮政编码查找县,我希望能够将该函数用作数组公式,以便我可以将它添加到 sheet 以进行表单响应.
我试图查找如何使该函数与数组公式一起使用,但我不太了解如何应用它。
是否可以将此代码用作数组公式?
样本sheet:
https://docs.google.com/spreadsheets/d/1S-TVchlE1sFTIOrbOL-v_N3LJJHLAHLJHkUtVpFeZok/edit#gid=0
/*
*
* Google Maps Formulas for Google Sheets
*
* Written by Amit Agarwal
*
* Web: https://www.labnol.org/google-maps-sheets-200817
*
*/
const md5 = (key = "") => {
const code = key.toLowerCase().replace(/\s/g, "");
return Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, key)
.map((char) => (char + 256).toString(16).slice(-2))
.join("");
};
const getCache = (key) => {
return CacheService.getDocumentCache().get(md5(key));
};
const setCache = (key, value) => {
const expirationInSeconds = 6 * 60 * 60; // max is 6 hours
CacheService.getDocumentCache().put(md5(key), value, expirationInSeconds);
};
/**
* Get the county name of an address on Google Maps.
*
* =GOOGLEMAPS_COUNTY("10 Hanover Square, NY")
*
* @param {String} address The address to lookup.
* @return {String} The county of the address.
* @customFunction
*/
const GOOGLEMAPS_COUNTY = (address) => {
if (!address) {
throw new Error("No address specified!");
}
if (address.map) {
return address.map("administrative_area_level_2");
}
const key = ["administrative_area_level_2", address].join(",");
const value = getCache(key);
if (value !== null) return value;
const { results: [data = null] = [] } = Maps.newGeocoder().geocode(address);
if (data === null) {
throw new Error("Address not found!");
}
const [{ long_name } = {}] = data.address_components.filter(
({ types: [level] }) => {
return level === "administrative_area_level_2";
}
);
if (!long_name) {
throw new Error("County not found!");
}
const answer = `${long_name}`;
setCache(key, answer);
return answer;
};
在您的情况下,以下示例脚本是您的预期目标吗?
示例脚本:
请将以下脚本添加到您当前的脚本中并保存。
const GOOGLEMAPS_COUNTY_ARRAY = values => values.map(r => r.map(c => c.toString() ? GOOGLEMAPS_COUNTY(c) : null));
使用本脚本时,请将自定义函数=GOOGLEMAPS_COUNTY_ARRAY(A2:A)
放入单元格。
测试:
使用您的示例电子表格时,将获得以下结果。