Google Apps Script中Drive.Files.List属性查询报错的解决方法
解决Google Drive API自定义属性查询的语法错误问题
问题出在查询字符串中properties has {...}部分的语法解析,Drive API对查询中的特殊字符(大括号)需要正确处理,以下是几种可行的修改方案:
方案1:转义大括号
在JavaScript字符串中,用\\{和\\}转义大括号,确保Drive API能正确解析查询语法:
Drive.Files.list({ "corpora": "allDrives", "includeTeamDriveItems": true, "q": "'1tfT0BtCg2Z7B7dviOqNQsZTdSjrX5V9B' in parents and trashed = false and properties has \\{key='photoId' and value='AJHPqhQHfoNqbZSdk8ZUU-RILziCVXViIWPKBG46RIcTVSqzijyVOwyvk1hRE-PxCv3LGXlcT7e7AacOUjKxrUdnG6ssS4YdnQ'\\}", "supportsTeamDrives": true })
方案2:使用模板字符串
用ES6模板字符串(反引号包裹)替代双引号字符串,无需手动转义单引号和大括号,代码更简洁:
Drive.Files.list({ "corpora": "allDrives", "includeTeamDriveItems": true, "q": `'1tfT0BtCg2Z7B7dviOqNQsZTdSjrX5V9B' in parents and trashed = false and properties has {key='photoId' and value='AJHPqhQHfoNqbZSdk8ZUU-RILziCVXViIWPKBG46RIcTVSqzijyVOwyvk1hRE-PxCv3LGXlcT7e7AacOUjKxrUdnG6ssS4YdnQ'}`, "supportsTeamDrives": true })
方案3:替换引号类型
将键值对的单引号换成双引号,并转义查询字符串中的双引号,避免语法冲突:
Drive.Files.list({ "corpora": "allDrives", "includeTeamDriveItems": true, "q": "'1tfT0BtCg2Z7B7dviOqNQsZTdSjrX5V9B' in parents and trashed = false and properties has {key=\"photoId\" and value=\"AJHPqhQHfoNqbZSdk8ZUU-RILziCVXViIWPKBG46RIcTVSqzijyVOwyvk1hRE-PxCv3LGXlcT7e7AacOUjKxrUdnG6ssS4YdnQ\"}", "supportsTeamDrives": true })
以上三种方案都能解决Invalid query错误,推荐使用方案2,代码可读性更高。
内容的提问来源于stack exchange,提问作者Bran H
相关产品推荐
相关产品推荐

