如何使用ONLYOFFICE宏分析电子表格数据

2023年09月08日作者: Mona

虽然 ChatGPT 4 现在有了增强的功能,但一些用户仍然更喜欢 ChatGPT 3.5,因为它的令牌成本较低。 然而,ChatGPT 3.5缺少文件上传功能,导致用户无法分析数据表。 在这篇文章中,我们会演示如何创建克服此限制的 ONLYOFFICE 宏,使您能够使用 OpenAI API 分析电子表格。

如何使用ONLYOFFICE宏分析电子表格数据

关于宏

为了解决这个限制,我们的宏遵循以下步骤:

  1. 它从电子表格中收集选定的单元格值。
  2. 将这些值编译成数组。
  3. 将此数组转换为字符串。
  4. 使用 fetch 请求将其发送到 Node.js 代理服务器。
  5. 服务器从请求正文中检索数组。
  6. 然后,它利用 OpenAI 库向 OpenAI 发送 API 请求。
  7. 收到响应后,服务器将其发送回响应对象内的宏。

有关设置代理服务器的详细说明(包括完整代码),请查看我们的博客文章,演示了如何创建宏以使用 OpenAI 的数据填充电子表格。

请注意!由于 ChatGPT 3.5 模型的令牌限制为 4096 个令牌,因此该宏最适合中型表(通常约为 50 行)。

构建宏

首先,我们检索电子表格中当前选定的范围:

// Get the selected range using ONLYOFFICE API
var selection = Api.GetSelection();

然后我们创建一个名为 rowData 的空数组来存储将从所选单元格收集的数据:

  // Initialize an array to store all data
  var rowData = [];

我们使用 ForEach 循环迭代选定范围中的每个单元格。 对于每个单元格,我们使用 GetValue 方法检索其值,然后将该值添加到 rowData 数组中:

// Use ForEach to iterate through the selected range
  selection.ForEach(function (cell) {
    // Retrieve the cell value using ONLYOFFICE API
  var cellValue = cell.GetValue();
    // Add cell value to the rowData array
    rowData.push(cellValue);
  });

之后,我们将 rowData 数组中收集的值转换为单个字符串,其中值之间用逗号分隔:

 // Merge the values in rowData and separate them by commas
  var rowDataAsString = rowData.join(',');

我们创建一个名为 requestData 的对象:

 // Prepare the data to send in the POST request
  var requestData = {
    prompt: `analyze this data ${rowDataAsString}`, // Use the merged string here
    apiKey: '<APIkey>', // Replace with your API key
  };
  • 提示字段包含用于分析的合并的单元格值字符串。
  • apiKey 字段包含 Node.js 服务器用于验证传入提取请求的 API 密钥。

然后我们使用 fetch 函数向指定的 URL 发送 POST 请求:

 fetch('http://localhost:3000/completion', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(requestData),
  })

我们处理 API 响应的方法是,首先将其转换为 JSON 格式,将数据记录到控制台,并在 POST 请求出现任何问题时实施错误处理:

.then(function (response) {
      return response.json();
    })
    .then(function (data) {
      // Log the API response
      console.log(data);
    })
    .catch(function (error) {
      // Handle any errors that occur during the fetch
      console.error('Error:', error);
    });

整个宏代码如下:

(function()
{
   // Get the selected range using ONLYOFFICE API
  var selection = Api.GetSelection();
  // Initialize an array to store all data
  var rowData = [];
  // Use ForEach to iterate through the selected range
  selection.ForEach(function (cell) {
    // Retrieve the cell value using ONLYOFFICE API
  var cellValue = cell.GetValue();
    // Add cell value to the rowData array
    rowData.push(cellValue);
  });
  // Merge the values in rowData and separate them by commas
  var rowDataAsString = rowData.join(',');
  // Prepare the data to send in the POST request
  var requestData = {
    prompt: `analyze this data ${rowDataAsString}`, // Use the merged string here
    apiKey: '<APIkey>', // Replace with your API key
  };
  // Send the data to the API (replace the URL with your OpenAI API endpoint)
  fetch('http://localhost:3000/completion', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(requestData),
  })
    .then(function (response) {
      return response.json();
    })
    .then(function (data) {
      // Log the API response
      console.log(data);
    })
    .catch(function (error) {
      // Handle any errors that occur during the fetch
      console.error('Error:', error);
    });
})();

现在,让我们运行我们的宏,看看它是如何工作的!

财务数据表示例:

员工数据表示例:

我们希望这篇博文中分享的见解能够有助于提高工作效率。 我们鼓励您在日常工作中探索和实施我们的各种 API 方法。

如果您有任何疑问或想法,请随时与我们分享。 我们对合作的可能性持开放态度和热情。 祝您的探索之路好运!