5

I have placed hyperlinks in an excel spreadsheet though my ruby-on-rails application. The links are to some privileged pages that require, After the login I am supposed to taken to the requested page. However, what happens is that after login I lang on the home page of the website. Interestingly, when I right-click the link in the excel and paste the link in the web-browser url, it works as expected. So I don't think it's my app's fault, but rather something in excel that I am missing?

My scenario is pretty much the same as this scenario: http://www.geekstogo.com/forum/topic/289186-excel-2007-hyperlink-loads-web-login-screen-not-linked-urlplease-help-me/

Saad Rehman Shah
  • 926
  • 2
  • 10
  • 28
  • Are you sure that the hyperlink text and URL ae identical? It's possible that the text shows the full address (so it works when copy-pasted) but the underlying URL is malformed? – Widor Feb 21 '12 at 11:06
  • The way I am copying it is right-clicking it in excel-sheet and copy-pasting Everything in the Address field to the browser, which works. If already signed in, copy pasting the link takes me to the page, but clicking the same in excel takes me once to a login page (and then to home page.) – Saad Rehman Shah Feb 21 '12 at 11:10
  • Possible duplicate of [Why are cookies unrecognized when a link is clicked from an external source (i.e. Excel, Word, etc...)](https://stackoverflow.com/questions/2653626/why-are-cookies-unrecognized-when-a-link-is-clicked-from-an-external-source-i-e) – IMSoP Jul 16 '18 at 12:17

4 Answers4

11

This issue normally happens in IE and i also faced the same problem.

Solution to this is very simple

  1. Create a redirect.html page in your public folder (public folder because you are using ROR)
  2. Copy this to your redirect.html

<html>
<body>
Please wait, loading your page... 
<script type="text/javascript">
function getQuerystring(key) {
key = key.replace(/[\[]/,"\\\[").replace(/    [\]]/,"\\\]");
var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
var query = regex.exec(window.location.href);
return query[1];
}
window.location = window.location.protocol + "//" + window.location.host + "/" + getQuerystring('page');
</script>
</body>
</html>

  1. The links you are constructing in excel should be modified

    For e.g Old link - http://test.com/post/comments/1 New link should be - http://test.com/redirect.html?page=post/comments/1

  2. You need to pass the part of URL (after base url) as a param

How this works.

When user clicks a link in excel it points to redirect.html and the javascript constructs the actual URL and redirects again to proper page, user will be navigated to actual page if already logged in else redirected to Login/Home page.

Nazar Hussain
  • 5,102
  • 6
  • 40
  • 67
Bharath
  • 156
  • 5
  • For an updated redirect.html, with Web APIs instead of regexes, see https://stackoverflow.com/a/70969243/685715 – Matt Wallis Feb 03 '22 at 10:05
1

This is a problem with Excel's internal URL handling, which has issues with modern web design patterns (e.g. sessions + redirects).

Here's a client-side solution that bypasses Excel's internal mechanisms and uses the OS default URL handler instead. Note that since it uses macros, this approach requires appropriate security settings.

In your worksheet's VBA module, add the following code:

Option Explicit

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
    ByVal hWnd As Long, _
    ByVal Operation As String, _
    ByVal Filename As String, _
    Optional ByVal Parameters As String, _
    Optional ByVal Directory As String, _
    Optional ByVal WindowStyle As Long = vbMinimizedFocus _
) As Long

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    ShellExecute 0, "Open", Target.Address
End Sub

This is based on the answers of two other SO answers.

Community
  • 1
  • 1
Florian Brucker
  • 9,621
  • 3
  • 48
  • 81
1

Not sure it's really an answer but I had the same problem with my application.

The whole application, including the home page, is protected (I'm using Devise). So whenever a user wants to access http://myapp, it redirects him to http://myapp/users/sign_in.

I think Devise uses a 301 or a 302 to redirect to the login screens.

My finding is that links clicked in Office and opening in IE cannot accomodate this redirect (no problem when Chrome is the default browser). Does it match your setup?

Ultimately, I have found no other solution but to link directly to the sign-in page... Maybe there are other options but I'm still looking for them.

EDIT: found this article (from 2006) about a bug in Outlook which totally matches our situation. Again, not a solution, but at least an explanation.

Pierre
  • 8,368
  • 4
  • 34
  • 50
  • 1
    Indeed Chrome is my default browser. This error is reproducible on both Firefox and Chrome. I haven't checked it on IE though. Thanks for your help! Do post any solutions you come up with. – Saad Rehman Shah Feb 22 '12 at 05:38
  • The bug mentioned matches exactly our situation. I don't have access to the server and the other solution proposed by @Bharath doesn't work for me. So this answer isn't an answer but at least it's an explanation, it should be marked valid. Thanks Pierre for the research. – Ronan Jouchet Jul 11 '12 at 17:43
  • MS QA is not exactly easy to reach. I did my best and posted the issue on [a related forum post](http://forums.asp.net/t/1587925.aspx/2/10?Excel+Hyperlink+Session+Problem). I hope the bug will make its way from the ASP.NET forum to Excel engineering. If you know better way to contact them, feel free to do so! – Ronan Jouchet Jul 11 '12 at 18:34
0

I have updated the redirect.html in the answer by @Bharath, replacing the regex manipulation with the Web APIs now available:

See the original answer by @Bharath for further explanation.

<!DOCTYPE html>
<html lang="en">
<body>
One moment please. We are trying to connect you ...
<script type="text/javascript">
    const url = new URL(window.location.href);
    const params = new URLSearchParams(url.search);
    const redirectTo = params.get('page');
    location = `${url.origin}/${redirectTo}`;
</script>
</body>
</html>

Of course, there is scope to handle errors (e.g. if (params.has('page')) etc.

See also Easy URL Manipulation with URLSearchParams by Eric Bidelman of Google.

Matt Wallis
  • 873
  • 5
  • 25