Link to home
Create AccountLog in
Avatar of bsharath
bsharathFlag for India

asked on

Post questions from excel to my site

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
Avatar of bsharath
bsharath
Flag of India image

ASKER

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
Avatar of Hagay Mandel
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?
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
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.
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
Thanks as mentioned anything from front end?
Feeds is front end.
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?
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.
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"
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 ?
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.
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
I could see the category was changed to 1
 thats anoynomus and the browser closed
It even clicked the save button and then the category was not accepted and turned red and exitted
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

Ah, just saw your previous posts. Maybe subcategory is required as well?
If you don't want the browser to close put a breakpoint on the line "objIE.Quit"
Can you give me the full code in one snippet please i get a code error message

sub category is not mandate
ASKER CERTIFIED SOLUTION
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
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?
Can you please attach the excel as for me the categories is where its failing to post
Sure, is it ok if the site name is in the excel?
Please remove it and upload
Ok here it is.
tsta.xls
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
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.
I have IE9 and Office 2007
I commented the FireEvent line but no change
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.
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?
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
Also would be great if you can help with this
https://www.experts-exchange.com/questions/27975089/Help-to-Modify-this-code-so-it-works-for-this-website.html

Very similar front end help but when free
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...
I have placed more info here
https://www.experts-exchange.com/questions/27975089/Help-to-Modify-this-code-so-it-works-for-this-website.html?anchorAnswerId=38723147#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
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.
Ok Thanks