Lianja
Lianja
XLSX Spreadsheet in AI Query
See more AI Examples
This example shows how to convert a .xlsx spreadsheet to CSV text for input in an AI query. Currently, most AIs can't handle Excel file inputs directly. If the spreadsheet is small, you can convert it to CSV text and use it as a text input.Chilkat Lianja Downloads
llSuccess = .F.
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// .xlsx files are Zip archives
loZip = createobject("CkZip")
llSuccess = loZip.OpenZip("qa_data/excel/fakeCompanies.xlsx")
if (llSuccess = .F.) then
? loZip.LastErrorText
release loZip
return
endif
loCsv = createobject("CkCsv")
loSheetNames = createobject("CkStringTable")
llSuccess = loCsv.XlsxGetSheets(loZip,loSheetNames)
if (llSuccess = .F.) then
? loCsv.LastErrorText
release loZip
release loCsv
release loSheetNames
return
endif
if (loSheetNames.Count = 0) then
? "There are no sheets in the .xlsx"
release loZip
release loCsv
release loSheetNames
return
endif
// Get the name of the 1st sheet.
lcSheetName = loSheetNames.StringAt(0)
// Load the 1st sheet into the CSV.
// We could've also loaded the 1st sheet by passing an empty string for the sheet name.
llSuccess = loCsv.XlsxLoadSheet(loZip,lcSheetName)
if (llSuccess = .F.) then
? loZip.LastErrorText
release loZip
release loCsv
release loSheetNames
return
endif
loSbCsv = createobject("CkStringBuilder")
loCsv.SaveToSb(loSbCsv)
// ------------------------------------------
loAi = createobject("CkAi")
loAi.Provider = "openai"
// Use your provider's API key.
loAi.ApiKey = "MY_API_KEY"
// Choose a model.
loAi.Model = "gpt-4o"
// Add text inputs
loAi.InputAddText("Describe what is contained in the following CSV data.")
loAi.InputAddTextSb(loSbCsv)
// Ask the AI for text output.
llSuccess = loAi.Ask("text")
if (llSuccess = .F.) then
? loAi.LastErrorText
release loZip
release loCsv
release loSheetNames
release loSbCsv
release loAi
return
endif
// Get the text response.
loSbResponse = createobject("CkStringBuilder")
loAi.GetOutputTextSb(loSbResponse)
? loSbResponse.GetAsString()
// Sample output:
// The CSV data contains information about five companies, including the following fields:
//
// 1. **CompanyName**: The name of the company.
// 2. **Address**: The street address of the company.
// 3. **City**: The city where the company is located.
// 4. **State**: The state where the company is located, abbreviated.
// 5. **Zip**: The ZIP code for the company's location.
// 6. **Phone**: The phone number of the company.
//
// Each row in the dataset corresponds to a different company with details provided for each of these fields:
//
// ...
// ...
// -------------------------------------------------------------
// The response is in markdown format.
// Also see Markdown to HTML Conversion Examples.
// -------------------------------------------------------------
release loZip
release loCsv
release loSheetNames
release loSbCsv
release loAi
release loSbResponse