Post questions from excel to my site

bsharath
bsharath used Ask the Experts™
on
Hello friends,

I have a Urgent request.

My friend ended up deleting 100's of questions from this site.

http://tinyurl.com/d57ffgs

Please note i have made this URL tiny so the site name is not shown open

Please use this account to login
User name : EE-Test
Pass : EE-Test


We have the excel with these details

Title | Category | body of the question

I want help with a script that gets posted when run macro into the logged in user account

We need to get this done as easily as possible and as soon as possible and would need help please.

regards
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Website is drupal and Mysql is the database

This link is fine

http://tinyurl.com/d57ffgs
Need to login to get to the page

Its a secured user account so will disable it after some time


We can do it with excel i guess but not sure

thanks for your help
I guess your final solution is to create a simple module, that only has admin setting page, and in which, you'll be able to run a script that will read the Excel file row by row, and insert the row to the database according to the database schema.
Optionally, you can write the same script in a file external to Drupal, but then, you'll have to make sure that you take care on ALL related data, such as additional columns  that Drupal generates, the revision table etc.

Which Drupal version?

Author

Commented:
Thanks
We have 6.27 version

Module i dont know but if we can do with a imacro/excel macro or some vb script would be fine
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Most Valuable Expert 2011
Top Expert 2016

Commented:
If MySQL is the data base and the Excel spreadsheet contains the information you want to load into the data base, you can save the Excel spreadsheet in csv format, and you can run a PHP script to read the csv file with fgetcsv().  This will give you an array of data elements that can be used in a MySQL query.  Each time your script calls fgetcsv() you will get one line of the csv file, which usually corresponds to one row of the data base table.

Author

Commented:
Thanks but any way to do from the front end?
Rather than database?
Use Feeds http://drupal.org/project/feeds. It is a fantastic module that works in both 6 and 7. If you run into issues mapping some of the fields, look up the modules that work with Feeds, such as Feeds Tamper.

Be sure the data is CSV with LF line endings, UTF-8. You'll get the best results that way.

Cheers,

Mike

Author

Commented:
Thanks as mentioned anything from front end?
Feeds is front end.
Robert SchuttSoftware Engineer

Commented:
There have been some suggestions that are probably better but you seem to really want to do this as if you were typing it in on the website. This should be possible, I have made a first attempt at this. Tested up until the point where a question would be submitted. Before I do that I want to ask you to clarify some things:
- the ckeditor seemed harder to manipulate than the plain text body, would it be ok to use the latter? (I already coded the switch-over)
- does the list you have contain an id or text value for category?
- when a category is selected, a sub-category appears. Can this stay empty?
Robert SchuttSoftware Engineer

Commented:
Here is the code so far, I took out the website url. If you want to test it use stepping through or put in some breakpoints, like I said it's only tested up to the submit code near the end of the while-wend loop.
Option Explicit

Const C_URL = "http://www.**************.com"
Const C_URL1 = C_URL & "/user"
Const C_URL2 = C_URL & "/create/question"

Const C_USER = "EE-Test"
Const C_PASS = "EE-Test"

Dim objIE As InternetExplorer

Sub DoProcess()
    Set objIE = New InternetExplorer
    objIE.Visible = True

    Dim h As HTMLDocument
    Dim f As HTMLFormElement
    Dim fe As HTMLFormElement
    Dim fsb As HTMLInputButtonElement

    ' login
    objIE.navigate C_URL1
    DoWait
    Set h = objIE.document
    Set f = h.forms("user-login")
    If Not f Is Nothing Then
        For Each fe In f.elements
            If fe.Name = "name" Then fe.innerText = C_USER
            If fe.Name = "pass" Then fe.innerText = C_PASS
        Next
        f.submit
        DoWait
        Set f = Nothing
    End If
    Set h = Nothing

    Dim rw As Integer ' row
    rw = 2
    ' now post questions
    While ActiveSheet.Cells(rw, 1) <> ""
        Debug.Print "row: " & rw
        objIE.navigate C_URL2
        DoWait
        Set h = objIE.document
        h.getElementById("switch_edit-body").Click
        DoWait
        Set fsb = Nothing
        Set f = h.forms("question-form")(0) ' there are 2 forms with this ID/name (1 hidden), we need the 1st one
        For Each fe In f.elements
            Dim feName As String, feID As String
            feName = ""
            feID = ""
            On Error Resume Next
            feName = fe.Name
            feID = fe.ID
            If Err.Number <> 0 Then Err.Clear
            On Error GoTo 0
            Debug.Print "*Name:" & feName & "*ID:" & feID & "*"
            If feName = "title" Then fe.innerText = ActiveSheet.Cells(rw, 1)
            If feID = "edit-taxonomy-3_1" Then
                fe.selectedIndex = CLng(ActiveSheet.Cells(rw, 2))
                fe.FireEvent "onchange", fe
                DoWait
            End If
            If feID = "edit-taxonomy-3_2" Then ' subcategory is hidden
                fe.selectedIndex = 1 'CLng(ActiveSheet.Cells(rw, 2))
            End If
            If feName = "body" Then fe.innerText = ActiveSheet.Cells(rw, 3)
            If feID = "edit-submit" Then Set fsb = fe ' Save button
        Next
        If fsb Is Nothing Then
            Debug.Print "* form submit"
            f.submit
        Else
            Debug.Print "* Save click"
            fsb.Click
        End If
        DoWait
        Set fsb = Nothing
        Set f = Nothing
        Set h = Nothing
        rw = rw + 1
    Wend
    ' done
    objIE.Quit
    Set objIE = Nothing
End Sub

Sub DoWait()
    While objIE.Busy Or objIE.readyState <> READYSTATE_COMPLETE
        DoEvents
    Wend
End Sub

Open in new window

PS: to use this code you need to add 2 references to your Excel VBA project: "Microsoft HTML Object Library" and "Microsoft Internet Controls".

PPS: to keep it simple for now I assumed you have a category ID in the second column of the Excel sheet.

Author

Commented:
Thanks
Seems promising

Category name would be better but ID where can i find that?

Can i have success or failure mentioned back in column "D"

Author

Commented:
I did check the source and add the category id's but it was failing to add the category

Can we skip the one that failed for some reason and try next in such cases ?
Robert SchuttSoftware Engineer

Commented:
Ah, I wasn't thinking straight. I used selectedIndex which is not the same as the value of the category. When I look at the source there is a different list. The category selections are dynamically added to the page. Maybe the easiest would be to use a direct translation so Hardware=2, Linux=3 etc. If it's not in the list for some reason use 1 for Anonymous. This could be done either in code or in an Excel lookup. For example:
Function GetCatIndex(sCatName As String)
    Select Case sCatName
        Case "Hardware":
            GetCatIndex = 2
        Case "Linux":
            GetCatIndex = 3
        Case "Apple":
            GetCatIndex = 4
        ' ...
        Case "Windows OS":
            GetCatIndex = 18
        ' unknown -> Anonymous?
        Case Else:
            GetCatIndex = 1
    End Select
End Function

Open in new window

and then use that on line 61 of the posted code:
fe.selectedIndex = GetCatIndex(ActiveSheet.Cells(rw, 2))

Open in new window


If there are other reasons it could fail it should be possible to use "on error" to determine success or failure and put that in column D. Although you probably mean an error that's reported on the page itself. That might be more difficult, would have to scan the source of the page after submit to see if a popup is being shown?

Does the body text contain html? I'm not sure how it will end up. Maybe just a matter of testing.

Author

Commented:
Thanks
To start with i placed 1 in excel column "B" and ran the macro but still did not post and the IE window closed before i could see where it failed

The body can have html but to start with we can go with plain text

Author

Commented:
I could see the category was changed to 1
 thats anoynomus and the browser closed

Author

Commented:
It even clicked the save button and then the category was not accepted and turned red and exitted
Robert SchuttSoftware Engineer

Commented:
Here is a slightly nicer approach, replace lines 60-64 with:
            If feID = "edit-taxonomy-3_1" Then
                Dim fse As HTMLSelectElement, idx As Long
                Set fse = fe
                For idx = 1 To fse.Options.Length - 1
                    Debug.Print "** check option: " & idx & " - " & fse.Options(idx).Text
                    If fse.Options(idx).Text = ActiveSheet.Cells(rw, 2) Then
                        fse.selectedIndex = idx
                        Exit For
                    End If
                Next
                ' set default category?
                If fse.selectedIndex = 0 Then fse.selectedIndex = 1
                Set fse = Nothing
                'fe.selectedIndex = GetCatIndex(ActiveSheet.Cells(rw, 2))
                'fe.FireEvent "onchange", fe
                'DoWait
            End If

Open in new window

Robert SchuttSoftware Engineer

Commented:
Ah, just saw your previous posts. Maybe subcategory is required as well?
Robert SchuttSoftware Engineer

Commented:
If you don't want the browser to close put a breakpoint on the line "objIE.Quit"

Author

Commented:
Can you give me the full code in one snippet please i get a code error message

sub category is not mandate
Software Engineer
Commented:
Ok, I just posted 2 test questions using this code:
Option Explicit

Const C_URL = "http://www.**************.com"
Const C_URL1 = C_URL & "/user"
Const C_URL2 = C_URL & "/create/question"

Const C_USER = "EE-Test"
Const C_PASS = "EE-Test"

Dim objIE As InternetExplorer

Sub DoProcess()
    Set objIE = New InternetExplorer
    objIE.Visible = True

    Dim h As HTMLDocument
    Dim f As HTMLFormElement
    Dim fe As HTMLFormElement
    Dim fsb As HTMLInputButtonElement

    ' login
    objIE.navigate C_URL1
    DoWait
    Set h = objIE.document
    Set f = h.forms("user-login")
    If Not f Is Nothing Then
        For Each fe In f.elements
            If fe.Name = "name" Then fe.innerText = C_USER
            If fe.Name = "pass" Then fe.innerText = C_PASS
        Next
        f.submit
        DoWait
        Set f = Nothing
    End If
    Set h = Nothing

    Dim rw As Integer ' row
    rw = 2
    ' now post questions
    While ActiveSheet.Cells(rw, 1) <> ""
        Debug.Print "row: " & rw
        objIE.navigate C_URL2
        DoWait
        Set h = objIE.document
        h.getElementById("switch_edit-body").Click
        DoWait
        Set fsb = Nothing
        Set f = h.forms("question-form")(0) ' there are 2 forms with this ID/name (1 hidden), we need the 1st one
        For Each fe In f.elements
            Dim feName As String, feID As String
            feName = ""
            feID = ""
            On Error Resume Next
            feName = fe.Name
            feID = fe.ID
            If Err.Number <> 0 Then Err.Clear
            On Error GoTo 0
            Debug.Print "*Name:" & feName & "*ID:" & feID & "*"
            If feName = "title" Then fe.innerText = ActiveSheet.Cells(rw, 1)
            If feID = "edit-taxonomy-3_1" Then
                Dim fse As HTMLSelectElement, idx As Long
                Set fse = fe
                For idx = 1 To fse.Options.Length - 1
                    Debug.Print "** check option: " & idx & " - " & fse.Options(idx).Text
                    If fse.Options(idx).Text = ActiveSheet.Cells(rw, 2) Then
                        fse.selectedIndex = idx
                        Exit For
                    End If
                Next
                ' set default category
                If fse.selectedIndex = 0 Then fse.selectedIndex = 1
                Set fse = Nothing
                fe.FireEvent "onchange", fe
                DoWait
            End If
            If feID = "edit-taxonomy-3_2" Then ' subcategory is hidden initially
                fe.selectedIndex = 1
            End If
            If feName = "body" Then fe.innerText = ActiveSheet.Cells(rw, 3)
            If feID = "edit-submit" Then Set fsb = fe ' Save button
        Next
        If fsb Is Nothing Then
            Debug.Print "* form submit"
            f.submit
        Else
            Debug.Print "* Save click"
            fsb.Click
        End If
        DoWait
        Set fsb = Nothing
        Set f = Nothing
        Set h = Nothing
        rw = rw + 1
    Wend
    ' done
    objIE.Quit
    Set objIE = Nothing
End Sub

Sub DoWait()
    While objIE.Busy Or objIE.readyState <> READYSTATE_COMPLETE
        DoEvents
    Wend
End Sub

Open in new window

The way errors are shown (like question is too short) may be nice for the users but not so nice in this case. Here's a screenshot of my test data and breakpoints:capture
Robert SchuttSoftware Engineer

Commented:
sub category is not mandate
ok I did put the code for that back in the previous post but either way shouldn't matter I guess. Probably for testing (like I did at first) using a question that contains too few words is what went wrong earlier?

Author

Commented:
Can you please attach the excel as for me the categories is where its failing to post
Robert SchuttSoftware Engineer

Commented:
Sure, is it ok if the site name is in the excel?

Author

Commented:
Please remove it and upload
Robert SchuttSoftware Engineer

Commented:
Ok here it is.
tsta.xls

Author

Commented:
Even now its the same as mine

Question category is required is the message i get

After the category is set and the content in the body and while submitting the category gets resetting to default and the site does not accept it as no category is selected
Robert SchuttSoftware Engineer

Commented:
How odd. I assume you have seen the questions I posted there? I'm using IE8 and an old Excel (2002), maybe that makes a difference? You could try fiddling with the code like take out the FireEvent.

Author

Commented:
I have IE9 and Office 2007
I commented the FireEvent line but no change
Robert SchuttSoftware Engineer

Commented:
Anything that you can find out what's happening maybe from setting a Watch in the code or looking at the html source could help.

Obviously it's not happening on my system or those posts would not have gone through either.

I think I can try it out on a configuration similar to yours tomorrow but will not have much time during the day.
Robert SchuttSoftware Engineer

Commented:
Actually one more thing you could try: set IE8 compatibility. After the IE window has opened, activate it and press F12, then change the browser mode (in the menu bar of the Dev Tools). It could change the way the category selection works. If it's your friend's website he may know what's going on?

Author

Commented:
I just tried on IE7/IE8/IE9 all 3 systems i have the same issue

For some reacon the categories first time is set fine and while saving the categories change back
Attached screenshot
Category.JPG

Author

Commented:
Also would be great if you can help with this
http://www.experts-exchange.com/Programming/Misc/Q_27975089.html

Very similar front end help but when free
Robert SchuttSoftware Engineer

Commented:
Looks like the ".FireEvent" is necessary (copies the value to a hidden input) and not working in Excel 2007 (and/or in combination with IE9). Did you test with that line commented out or with the FireEvent active?

Maybe it's possible to fill the hidden field directly, could be much easier than finding out what's wrong with the current code, I'll have another look later on.

I did see that other question when you posted it but frankly didn't really understand what you were after, so I skipped it at the time...

Author

Commented:
I have placed more info here
http://www.experts-exchange.com/Programming/Misc/Q_27975089.html#a38723147

FireEvent it was commented while i checked in machine IE8 and now i have removed the comment and it works perfectly

Thanks a lot i can survive with IE8 and current functionality

thanks a lot for your help much appretiated...
:-)

Please have a look at the other post when possible
Robert SchuttSoftware Engineer

Commented:
Great!
Robert SchuttSoftware Engineer

Commented:
I had a look but no ideas so far, it's a completely different approach and I can't see a direct way to integrate the login from my code with that way of downloading data from the website.

Author

Commented:
Ok Thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial