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

如何通过Google Apps Script查询Google Drive中的SQLite文件?

如何将SQLite查询结果从HTML页面传回Google Apps Script主文件?

问题背景

我在Google Drive中存储了一个SQLite数据库文件,通过Google表格的Google Apps Script结合HTML页面和sql.js实现了数据库查询,但不知道如何将查询结果传回.gs主文件,进而填充表格的指定行。现有代码如下:

.gs文件代码

function onOpen() {
  const ui = SpreadsheetApp.getUi();
  ui.createMenu('Integrations')
      .addItem('SQLite','sqlite')
      .addToUi();
}

function sqlite() {
  const ui = SpreadsheetApp.getUi();
  const html = HtmlService.createHtmlOutputFromFile('sqlite').setTitle('SQLite');
  ui.showModalDialog(html, ' ');
}

function getDriveFile() {
    return DriveApp.getFilesByName('filename.db').next().getBlob().getBytes();
}

HTML页面代码

<html>  
  <head>
    <script src="https://cdn.jsdelivr.net/npm/sql.js@1.8.0/dist/sql-wasm.min.js"></script>
    <script>
      // Async call to the DriveApp that loads the SQLite file
      let SQL, db, file;
      (async() => {
        SQL = await initSqlJs({ locateFile: file => 'https://cdn.jsdelivr.net/npm/sql.js@1.8.0/dist/' + file });
        google.script.run.withSuccessHandler(buffer => {
          db = new SQL.Database(new Uint8Array(buffer));
          const stmt = db.prepare("SELECT * FROM TABLE WHERE ID=:id");
          const result = stmt.getAsObject({':id' : 1});
          console.log(result);
          // 疑问:如何将结果传回主gs文件?
          google.script.host.close() 
        }).getDriveFile();
      })();
    </script>
    </head>
  <body>
      ...
  </body>
</html>

解决方案

核心思路是利用google.script.run调用.gs文件中新增的接收函数,将查询结果传递过去,再在该函数中完成表格填充操作。

步骤1:在.gs文件中新增结果处理函数

添加一个用于接收查询结果并写入表格的函数,可根据需求指定写入的工作表和单元格范围:

function writeToSheet(result) {
  // 选择要写入的工作表(替换成你的工作表名称)
  const targetSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
  // 按需求整理数据顺序(推荐手动指定字段,避免对象键顺序异常)
  const rowData = [
    result.ID,
    result.字段1, // 替换为你的实际字段名
    result.字段2  // 替换为你的实际字段名
  ];
  // 将数据写入第2行第1列开始的单元格(可根据需求修改行列号)
  targetSheet.getRange(2, 1, 1, rowData.length).setValues([rowData]);
}

步骤2:修改HTML页面的脚本传递结果

在HTML的JavaScript代码中,拿到查询结果后,调用上述新增的writeToSheet函数传递数据,并可添加成功回调关闭弹窗:

<script>
  // Async call to the DriveApp that loads the SQLite file
  let SQL, db, file;
  (async() => {
    SQL = await initSqlJs({ locateFile: file => 'https://cdn.jsdelivr.net/npm/sql.js@1.8.0/dist/' + file });
    google.script.run.withSuccessHandler(buffer => {
      db = new SQL.Database(new Uint8Array(buffer));
      const stmt = db.prepare("SELECT * FROM TABLE WHERE ID=:id");
      const result = stmt.getAsObject({':id' : 1});
      console.log(result);
      // 传递结果到gs文件的writeToSheet函数
      google.script.run.withSuccessHandler(() => {
        google.script.host.close(); // 成功写入后关闭弹窗
      }).writeToSheet(result);
    }).getDriveFile();
  })();
</script>

注意事项

  • 若查询结果为多条数据,可将结果数组传递给writeToSheet,并修改函数使用setValues批量写入多行。
  • 手动指定字段顺序比直接使用Object.values(result)更可靠,避免因对象键顺序变化导致数据错位。
  • 确保SQLite数据类型与Google表格单元格类型兼容,必要时在writeToSheet中做类型转换。

内容的提问来源于stack exchange,提问作者user2013861

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 16:05:41