超链接未显示正确文本

2 投票
2 回答
1000 浏览
提问于 2025-04-16 19:12

我想在一个单元格里添加一个超链接,点击后可以打开一个文件夹。下面的代码可以在正确的单元格里创建超链接,并且点击后能正确跳转到那个文件夹,但它没有显示我提供的文本,而是显示了文件夹的路径,比如说(:C:\Documents and Settings\abulle\Desktop\Python-Stuff\Spec-Load\Formatted\),而不是我想要的'Folder'。

sheet.Hyperlinks.Add( Anchor = sheet.Cells(7,21), Address = "C:\\Python-Stuff\\Spec-Load\\Formatted\\" , TextToDisplay = "Folder")

2 个回答

0

试试这个

 def addHyperlink(self, uri_1, summa_, sheetId, rowIndex, colIndex):
    requests = []
    requests.append({
        "updateCells": {
            "rows": [{
                "values": [{
                    'userEnteredValue': {'numberValue': floatG(summa_)},  #
                    'effectiveValue': {'numberValue': floatG(summa_)},
                    'formattedValue': "р." + summa_,
                    'userEnteredFormat': {
                        'numberFormat': {'type': 'NUMBER', 'pattern': '[$р.-419]#,##0.00'},
                        'backgroundColor': {'red': 1, 'green': 1, 'blue': 0.6}, 'borders': {
                            'top': {
                                'style': 'SOLID', 'width': 1, 'color': {}, 'colorStyle': {'rgbColor': {}}},
                            'bottom': {
                                'style': 'SOLID', 'width': 1, 'color': {}, 'colorStyle': {'rgbColor': {}}},
                            'left': {
                                'style': 'SOLID', 'width': 1, 'color': {}, 'colorStyle': {'rgbColor': {}}},
                            'right': {
                                'style': 'SOLID', 'width': 1, 'color': {}, 'colorStyle': {'rgbColor': {}}}},
                        'horizontalAlignment': 'RIGHT', 'verticalAlignment': 'BOTTOM',
                        'textFormat': {
                            'foregroundColor': {'red': 0.06666667, 'green': 0.33333334, 'blue': 0.8},
                            'fontFamily': 'Arial', 'underline': True,
                            'foregroundColorStyle': {
                                'rgbColor': {'red': 0.06666667, 'green': 0.33333334, 'blue': 0.8}},
                            'link': {'uri': uri_1}
                        },
                        'hyperlinkDisplayType': 'LINKED',
                        'backgroundColorStyle': {'rgbColor': {'red': 1, 'green': 1, 'blue': 0.6}}
                    }
                    , 'hyperlink': uri_1, 'note': 'макс',
                }
                ]
            }], "fields": "userEnteredFormat(numberFormat,backgroundColor,horizontalAlignment,verticalAlignment,textFormat)", "start": {
                "sheetId": sheetId, "rowIndex": rowIndex, "columnIndex": colIndex}}})
    body = {
        "requests": requests}
    request = self.service.spreadsheets().batchUpdate(spreadsheetId=self.spreadsheetId, body=body)
    return request.execute()
3

我有一个临时的解决办法,算是个权宜之计。现在没时间去找更好的答案。如果这是我日常工作的一部分,我会花时间去弄清楚发生了什么。

我遇到过同样的问题(Excel显示链接地址作为单元格文本,而不是在Hyperlinks.Add()中提供的TextToDisplay值)。

我的代码在单元测试中运行时没问题,当我用Python 2.7解释器运行时,单元格中显示的是'TextToDisplay'参数的值。而在“生产”代码中(使用py2exe构建的),则显示的是超链接。我总有一天会弄清楚原因(这项工作优先级不高)。

Hyperlinks.Add会返回它刚刚添加的Hyperlink对象。解决方法是检查这个对象的TextToDisplay属性——如果不是我想要的值,我就把正确的值赋给它。

link = sheet.Hyperlinks.Add( Anchor = sheet.Cells(7,21),
                             Address = u"C:\\Python-Stuff\\Spec-Load\\Formatted\\" ,
                             TextToDisplay = u"Folder")
if link.TextToDisplay != u"Folder":
    link.TextToDisplay = u"Folder" # kludge

撰写回答