It is probably better and faster to use Feeds, but since the D8 version is still under development; alternatively, you could use Excel + VBA (Visual Basic for Applications, comes with Excel) + Internet Explorer 11.
Here an example how you can import your CSV content using VBA.
For example, lets assume you want to import this and create new nodes with the info from your CSV:
Here is a sample VBA code:
Sub Drupal_Import()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
Dim x As Integer
For x = 2 To 4 'this controls which rows get added, so only 2 to 4
myURL = "https://rgr79.ply.st/node/add/article"
With IE
.Visible = True 'makes your Internet Explorer window visible.
.navigate myURL
End With
Do
el = vbNullString
On Error Resume Next
Set HTML = IE.document
el = HTML.getElementsByClassName("visually-hidden")(1).innerText
DoEvents
Loop While el = vbNullString
'the above loops until the visualy-hidden class is detected, which means the edit form has been loaded
Application.Wait (Now + TimeValue("00:00:03")) 'tells the program to wait 3 secs.
Set HTML = IE.document
HTML.getElementById("edit-title-0-value").Value = Cells(x, 1).Value 'here the 1 is the Y (so 1 is Column A)
HTML.getElementById("edit-body-0-value").Value = Cells(x, 2).Value 'here the 2 is the Y (so 2 is Column B)
Cells(x, 3).Value = "Done" 'here we use the 3rd column (Column C) and mark it as Done to keep track.
HTML.getElementsByClassName("button js-form-submit form-submit")(1).Click 'clicks the submit button
Application.Wait (Now + TimeValue("00:00:00")) 'here I have a wait for 0, increase it to 2 or 3 if you see your VBA get stuck after submitting a node.
Do
el = vbNullString
On Error Resume Next
Set HTML = IE.document
el = HTML.getElementsByClassName("messages messages--status")(0).innerText
DoEvents
Loop While el = vbNullString
'all the above does is loops the code until the drupal message is detected, which means the node was loaded, after the submit.
Next x
End Sub
Make sure you change the domain name in myURL = "https://rgr79.ply.st/node/add/article"
line to your domain. I am using a simplytest.me domain, if you can't already tell.
How to add the VBA code?
Click on the Developer tab and then on Visual Basic icon (or ALT + F11)
and Paste the code inside of Sheet1(Sheet1)
Now in the toolbar, click on tool
and then References
You will need to scroll, find and checkmark
- Microsoft HTML Object Library
- Microsoft Internet Controls
Note: I know it works with Internet Explorer 11, not sure if it works with the new Microsoft Edge browser.
Now you are ready to run the script. You can do so by clicking the Play button
You could also run it by clicking the Macros Icon (see image2), but I prefer to do it from the VBA window.
So you hit the play button and an IE window automatically opens and you see this:
Oh yes, you forgot to log in, lol.
So you proceed to login to Drupal and then you close explorer (since cookies history saves your login) and plan on hitting the play button again. But you are not able to... you see the play button greyed out and not able to make any changes to the VBA code... What is going on?
Well your code is still running, so you need to hit the Stop (reset) button.
Now you can click the play button again and joy the world of automatization.
Important
If you plan to insert stuff to the Body field (like we are doing in this example), since Drupal 8 uses CKEditor for this field and CKEditor is JS, we cannot target a div class or ID; thus, we cannot add content inside CKEditor.
Fortunately, there is a work around. Make sure your IE 11 Security settings are set to High, this automatically will block all JS. Therefore, CKeditor will not load, and the body field will be just like the other fields.
If you need to edit nodes example:
Sub Drupal_Edit()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
Dim x As Integer
For x = 2 To 4 'this controls which rows get added, so only 2 to 4
myURL = "https://rgr79.ply.st/node/" & Cells(x, 3) & "/edit"
With IE
.Visible = True 'makes your Internet Explorer window visible.
.navigate myURL
End With
Do
el = vbNullString
On Error Resume Next
Set HTML = IE.document
el = HTML.getElementsByClassName("visually-hidden")(1).innerText
DoEvents
Loop While el = vbNullString
'the above loops until the visualy-hidden class is detected, which means the edit form has been loaded
Application.Wait (Now + TimeValue("00:00:04")) 'tells the program to wait 3 secs.
Set HTML = IE.document
HTML.getElementById("edit-title-0-value").Value = Cells(x, 1).Value 'here the 1 is the Y (so 1 is Column A)
HTML.getElementById("edit-body-0-value").Value = Cells(x, 2).Value 'here the 2 is the Y (so 2 is Column B)
Cells(x, 4).Value = "Done" 'here we use the 4th column (Column D) and mark it as Done to keep track.
HTML.getElementsByClassName("button js-form-submit form-submit")(1).Click 'clicks the submit button
Application.Wait (Now + TimeValue("00:00:00")) 'here I have a wait for 0, increase it to 2 or 3 if you see your VBA get stuck after submitting a node.
Do
el = vbNullString
On Error Resume Next
Set HTML = IE.document
el = HTML.getElementsByClassName("messages messages--status")(0).innerText
DoEvents
Loop While el = vbNullString
'all the above does is loops the code until the drupal message is detected, which means the node was loaded, after the submit.
Next x
End Sub