You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 22:06:10