瀏覽代碼

HUE-8089 [core] Automatically add HYPERLINK to links in Excel downloads

Romain Rigaux 7 年之前
父節點
當前提交
8ace1ea7fe
共有 1 個文件被更改,包括 4 次插入2 次删除
  1. 4 2
      desktop/core/src/desktop/lib/export_csvxls.py

+ 4 - 2
desktop/core/src/desktop/lib/export_csvxls.py

@@ -41,11 +41,13 @@ def nullify(cell):
   return cell if cell is not None else "NULL"
 
 
-def encode_row(row, encoding=None):
+def encode_row(row, encoding=None, make_excel_links=False):
   encoded_row = []
   for cell in row:
     if isinstance(cell, six.string_types):
       cell = re.sub(ILLEGAL_CHARS, '?', cell)
+      if make_excel_links:
+        cell = re.sub('(https?://.+)', r'=HYPERLINK("\1")', cell, flags=re.IGNORECASE)
     cell = nullify(cell)
     if not isinstance(cell, numbers.Number):
       cell = smart_str(cell, encoding or i18n.get_site_encoding(), strings_only=True, errors='replace')
@@ -101,7 +103,7 @@ def create_generator(content_generator, format, encoding=None):
 
       # Write row data to workbook
       for row in _data:
-        worksheet.append(encode_row(row, encoding))
+        worksheet.append(encode_row(row, encoding, make_excel_links=True))
         row_ctr += 1
 
     yield xls_dataset(workbook).xls