/** * 从小米链接中提取 sheetToken 和 range。 * * @param {string} url - 要提取数据的URL。 * @returns {{sheetToken: string, range: string | null} | null} - 包含 sheetToken 和 range 的对象,如果没有匹配则返回 null。 */ export const extractSheetIds = (url: string) => { // 定义匹配 wiki 和 sheets 两种URL格式的正则表达式 const pattern = /(?:wiki|sheets)\/([\w\d]+)(?:\?sheet=([\w\d]+))?/ const match = url.match(pattern) console.log("🚀 ~ extractSheetIds ~ match:", match) if (match) { return { sheetToken: match[1], // sheetToken 在组1 range: match[2] || null, // range 在组2,如果没有则返回 null } } return null // 如果没有匹配,则返回 null } /** * 校验链接是否合法 * @param {string} link - 网页链接 * @throws {Error} - 当链接为空或格式不正确时抛出错误 */ export const validateLink = (link: string): void => { if (!link) throw new Error("链接不能为空") try { new URL(link) } catch { throw new Error("链接格式不正确") } }