informationsea / xlsxwriter-rs

Excel file writer for Rust
https://crates.io/crates/xlsxwriter
Apache License 2.0
265 stars 44 forks source link

Paste a link with text alt in a cell #54

Closed k-bx closed 10 months ago

k-bx commented 10 months ago

I'd like to paste a text saying "Photo 1" which, upon click, leads to some photo link. It should look like this (in Ukrainian):

зображення

Is it possible via current means of xlsxwriter-rs?

jmcnamara commented 10 months ago

Do you mean you want to link to some image within the file or to some image at an external URL?

k-bx commented 10 months ago

External url opened in browser, basically a web page.

jmcnamara commented 10 months ago

The wrapped libxlsxwriter library doesn't have a function/method for writing a url and specifying an alternative string like the Perl/Python versions. Instead, the suggested workaround is to write the URL and then overwrite the cell with an alternative string with the same cell format.

Here is an xlsxwriter.rs example:

use xlsxwriter::{format::*, Workbook};

fn main() -> Result<(), xlsxwriter::XlsxError> {
    let workbook = Workbook::new("gh54.xlsx")?;

    let mut worksheet = workbook.add_worksheet(None)?;

    // Create a url format.
    let mut url_format = Format::new();
    url_format.set_font_color(FormatColor::Blue);
    url_format.set_underline(FormatUnderline::Single);

    let image_url = "https://docs.rs/-/rustdoc.static/rust-logo-151179464ae7ed46.svg";

    // Write a url.
    worksheet.write_url(0, 0, image_url, Some(&url_format))?;

    // Overwrite the displayed URL string with another string. Note, we need to
    // specify the format for the string to match the hyperlink.
    worksheet.write_string(0, 0, "Photo 1", Some(&url_format))?;

    workbook.close()
}

Output:

screenshot

Sample file: gh54.xlsx

k-bx commented 10 months ago

That's a clever hack, thank you! Created an upstream issue at https://github.com/jmcnamara/libxlsxwriter/issues/416 too

jmcnamara commented 10 months ago

That's a clever hack, thank you! Created an upstream issue at jmcnamara/libxlsxwriter#416 too

That isn't really a hack. It is the documented way of doing it in libxlsxwriter. It is also more or less what Excel does (it stores a link for the cell but also adds an additional text, or number, or formula, for the cell).

If it works for your use case you should close this issue.

k-bx commented 10 months ago

Oh, I see. I didn't check if it's possible in JS lib, but I was under an impression that the way they do it gives you an ability to insert multiple links via comma, like "Photo 1, Photo2, Photo3" all in single cell, each going to its own URL. I don't need it for now, can open a new issue later if needed.

jmcnamara commented 10 months ago

I was under an impression that the way they do it gives you an ability to insert multiple links via comma, like "Photo 1, Photo2, Photo3" all in single cell, each going to its own URL.

Unfortunately, that isn't possible in Excel. It only allows one URL per cell.