js获取选中文本的第一个字及最后一个字的偏移量

两种方法:
方法一:
mousedown时获取偏移量,mouseup时获取便宜量就是第一个字及最后一个字的坐标。
方法二:对选中的字画矩形。第一个矩形的坐标就是第一个字的坐标,最后一个字的坐标是x+width,y。
针对方法二给出如下代码:
在mouseup方法中:
var pageDiv=document.getElementById(“id”)//这段文本的id
var selection = that.getSelection(window);
let offsetLeft = that.getAbsLeft(pageDiv);
let offsetTop = that.getAbsTop(pageDiv);
var selectionRect = that.getSelectionRect(selection, offsetLeft, offsetTop);
console.log(selectionRect )
在scoll方法中:that.scrollTop = that.getDocumentScrollTop(document);
在methods中:
getDocumentScrollTop(document) {//页面的滚动高度
var scroll_top = 0;
if (document.documentElement && document.documentElement.scrollTop) {
scroll_top = document.documentElement.scrollTop;
} else if (document.body) {
scroll_top = document.body.scrollTop;
}
return scroll_top;
},
getAbsLeft(obj) {//距离左边的距离
var l = obj.offsetLeft;
while (obj.offsetParent != null) {
obj = obj.offsetParent;
l += obj.offsetLeft;
}
return l;
},
getAbsTop(obj) {//距离顶部的距离
var top = obj.offsetTop;
while (obj.offsetParent != null) {
obj = obj.offsetParent;
top += obj.offsetTop;
}
return top;
},
getSelection(root) {//选中的字获取(主要方法)
let t = null;
if (root.getSelection) {
t = root.getSelection();
} else if (root.document.getSelection) {
t = root.document.getSelection();
} else if (root.document.selection) {
t = root.document.selection.createRange().text;
}
return t;
},

        getSelectionRect(selection, offsetLeft, offsetTop) {//获取选中文字的位置(主要方法)。(坐标)
            if (selection.rangeCount <= 0) {
                return null;
            }
            if (selection.getRangeAt(0).getClientRects().length > 0) {
                let firstRect = selection.getRangeAt(0).getClientRects()[0];//第一个矩形
                let lastRect = selection.getRangeAt(0).getClientRects()[selection.getRangeAt(0).getClientRects().length - 1];//最后一个矩形
                let firstX = firstRect.x - offsetLeft;//其中offsetLeft是pdf距离左边的距离,offsetTop是pdf距离顶部的距离。
                let firstY = firstRect.y + this.scrollTop - offsetTop;//第一个字的纵坐标
                let lastX = lastRect.x + lastRect.width - offsetLeft;//最后一个字的横坐标
                let lastY = lastRect.y + this.scrollTop - offsetTop;//最后一个字的纵坐标
                let coordinate = {
                    firstX: firstX,
                    firstY: firstY,
                    lastX: lastX,
                    lastY: lastY
                }
                return coordinate //坐标是每个字的左上角
            }
        },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用`openpyxl`库来读取和处理Excel文件,以下是一个示例程序,可以获取Excel表格的最后一行,并通过偏移量获取不同列的最后一个元素行: ```python import openpyxl # 打开Excel文件 wb = openpyxl.load_workbook('example.xlsx') # 获取当前活动的工作表 sheet = wb.active # 获取最大行数和最大列数 max_row = sheet.max_row max_col = sheet.max_column # 获取最后一行的数据 last_row = [] for col in range(1, max_col + 1): cell_value = sheet.cell(row=max_row, column=col).value last_row.append(cell_value) # 通过偏移量获取不同列的最后一个元素行 col_offset = 2 last_element_row = sheet.cell(row=max_row, column=max_col - col_offset + 1).value # 打印结果 print('最后一行的数据:', last_row) print('第{}列的最后一个元素行:{}'.format(max_col - col_offset + 1, last_element_row)) ``` 在这个示例程序中,我们打开了一个名为`example.xlsx`的Excel文件,并获取了当前活动的工作表。然后,我们使用`max_row`和`max_column`属性获取了最大行数和最大列数。 接下来,我们使用循环和`cell()`方法获取最后一行的数据,并将它们存储在一个列表`last_row`中。最后,我们使用`cell()`方法和偏移量`col_offset`获取了不同列的最后一个元素行,并打印了结果。 需要注意的是,这个示例程序只适用于所有列的数据类型都相同的情况。如果表格中存在不同类型的数据,需要根据实际情况进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值