Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

API Data Export in CSV With Label Format

  • rajkumar_dms
  • rajkumar_dms's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
1 day 16 hours ago #270160 by rajkumar_dms
API Data Export in CSV With Label Format was created by rajkumar_dms
Please help us help you and fill where relevant:
LimeSurvey version: [6.13]
Own server or LimeSurvey Cloud:Cloud
Survey theme/template:Twenty Three
==================
Here is the script for data export through API from Lime survey but when we run that for Single choice it shows only coded format data but i need it label format along with header of Question with question number along with its labeling

Function Decode64(base64String As String) As String
    Dim xml As Object
    Dim bytes() As Byte
    Dim result As String

    Set xml = CreateObject("MSXML2.DOMDocument")
    Dim element As Object
    Set element = xml.createElement("base64")
    element.DataType = "bin.base64"
    element.Text = base64String
    bytes = element.nodeTypedValue
    result = StrConv(bytes, vbUnicode)
    Decode64 = result
End Function

Sub Export_LimeSurvey()
    Dim key As String, limeuser As String, limepass As String, limeurl As String, URL As String
    Dim jsonText As String, jsonObject As Object
    Dim SurveyID As String, DocumentType As String
    Dim export64 As String, export64Decoded As String
    Dim objHTTP As Object, sendtext As String

    limeurl = " livesurvey.limesurvey.net/ "
    limeuser = "xxxx"
    limepass = "xxxxx"
    SurveyID = "855297"
    DocumentType = "csv"
    
    Cells.Clear
    
    Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
    URL = limeurl + "/admin/remotecontrol"

    ' Step 1: Get session key
    sendtext = "{""method"":""get_session_key"", ""params"": [""" & limeuser & """, """ & limepass & """],""id"": 1}"
    With objHTTP
        .Open "POST", URL, False
        .setRequestHeader "User-Agent", "Mozilla/4.0"
        .setRequestHeader "Content-type", "application/json"
        .Send sendtext
        jsonText = .responseText
    End With
    Set jsonObject = JsonConverter.ParseJson(jsonText)
    key = jsonObject("result")
    
    ' Step 2: Export responses
    sendtext = "{""method"":""export_responses"", ""params"": [""" & key & """, """ & SurveyID & """, """ & DocumentType & """],""id"": 1}"
    objHTTP.Send sendtext
    jsonText = objHTTP.responseText
    Set jsonObject = JsonConverter.ParseJson(jsonText)
    export64 = jsonObject("result")
    
    ' Step 3: Decode base64
    export64Decoded = Decode64(export64)
    
    ' Step 4: Release session key
    sendtext = "{""method"":""release_session_key"", ""params"": [""" & key & """],""id"": 1}"
    objHTTP.Send sendtext
    
    ' Step 5: Write to Excel
    Dim lines() As String
    lines = Split(export64Decoded, vbCrLf)
    
    Dim i As Integer
    For i = LBound(lines) To UBound(lines)
        Cells(i + 1, 1).Value = lines(i)
    Next i
    
    ' Step 6: Convert to CSV columns
    Columns("A:A").TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, Comma:=True
    
    Cells.WrapText = False
End Sub


Please make necessary change in the script as per my need

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 day 12 hours ago - 1 day 12 hours ago #270166 by Joffm
Replied by Joffm on topic API Data Export in CSV With Label Format
I do not see an issue, if you enter the correct parameters in function "export_responses"
 

Here I created a table with the results.
You will get
 

And I wonder why you use such a complicated approach.
If you choose "csv" as $sDocumentType you  get this
 

Well, all this is in php.
 

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 1 day 12 hours ago by Joffm.

Please Log in to join the conversation.

  • rajkumar_dms
  • rajkumar_dms's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
1 day 10 hours ago #270168 by rajkumar_dms
Replied by rajkumar_dms on topic API Data Export in CSV With Label Format
Thanks for your suggestion

This approach i found on forums that the reason i used it

Can share the script for simple approach so that i can export as it text

Please Log in to join the conversation.

Moderators: holchtpartner

Lime-years ahead

Online-surveys for every purse and purpose