我有一个字符索引,想要获取包含该索引的页面的SET search_path=tmp;
CREATE TABLE users (
id INTEGER NOT NULL,
email VARCHAR(120),
PRIMARY KEY (id),
UNIQUE (email)
);
INSERT INTO users VALUES(1,'Jim.Fake@nolive.co.uk');
INSERT INTO users VALUES(2,'osman.narnia@live.co.uk');
INSERT INTO users VALUES(3,'KimFake1@outlook.com');
INSERT INTO users VALUES(4,'jaakume@gmail.com');
INSERT INTO users VALUES(5,'omarblack@protonmail.com');
INSERT INTO users VALUES(6,'osman.everton@hotmail.com');
INSERT INTO users VALUES(7,'radoslaw@ganczarek.in');
INSERT INTO users VALUES(8,'kane1001@live.co.uk');
INSERT INTO users VALUES(9,'osman.soloking009@outlook.com');
INSERT INTO users VALUES(10,'Shum1945@fleckens.hu');
CREATE TABLE raffle (
id INTEGER NOT NULL,
user_id INTEGER,
colour VARCHAR(120),
up1 VARCHAR(4),
up2 VARCHAR(4),
PRIMARY KEY (id),
CONSTRAINT _color_up1_up2_uc UNIQUE (colour, up1, up2),
FOREIGN KEY(user_id) REFERENCES users (id)
);
INSERT INTO raffle VALUES(1,1,'Blue','7c4c','5c7e');
INSERT INTO raffle VALUES(2,1,'Pink','635d','853f');
INSERT INTO raffle VALUES(3,1,'Plum','5e80','7611');
INSERT INTO raffle VALUES(4,1,'Aqua','937c','1b75');
INSERT INTO raffle VALUES(5,2,'Navy','1d9a','8914');
INSERT INTO raffle VALUES(6,1,'Grey','d869','fc97');
INSERT INTO raffle VALUES(7,4,'Rose','5fee','b31f');
INSERT INTO raffle VALUES(8,1,'Ruby','d5b4','e749');
INSERT INTO raffle VALUES(9,2,'Teal','cf0b','3bf5');
INSERT INTO raffle VALUES(10,1,'Gold','98a7','3079');
INSERT INTO raffle VALUES(11,1,'Jade','5c69','66f8');
INSERT INTO raffle VALUES(12,1,'Lime','156f','6b34');
INSERT INTO raffle VALUES(13,1,'Blue','7da3','d95b');
INSERT INTO raffle VALUES(14,1,'Pink','a63e','b9b6');
INSERT INTO raffle VALUES(15,1,'Plum','d989','71a5');
INSERT INTO raffle VALUES(16,1,'Aqua','7372','0682');
/****
INSERT INTO raffle VALUES(17,9,'Navy','01b3','e444');
INSERT INTO raffle VALUES(18,1,'Grey','d679','0123');
INSERT INTO raffle VALUES(19,1,'Rose','5963','692d');
...
***/
。为了说明这个特定的用例,我正在运行句子验证规则(使用自然语言处理),我想在用户正在处理的整个页面上运行它们。所以:
Range
示例电话:
public static class ExtensionForDocument
{
public static Range GetPage(this Document document, int characterIndex)
{
throw new NotImplementedException();
}
}
我试过了:
public void OnInspect(IRibbonControl control)
{
var selection = Application.Selection;
var selectionRange = Application.ActiveDocument.GetPage(selection.Range.Start);
// Process range here.
}
部分不起作用。因此,获取实际页面的范围:
public static Range GetPage(this Document document, int characterIndex)
{
var sectionCount = document.Sections.Count;
for (var sectionIndex = 1; sectionIndex <= sectionCount; sectionIndex++)
{
var section = document.Sections[sectionIndex];
var sectionRange = section.Range;
if (characterIndex >= sectionRange.Start && characterIndex <= sectionRange.End)
{
return sectionRange;
}
}
return null;
}
但是在这些范围内,“开始”和“结束”表示页码,而不是字符索引。我也用书签尝试过相同的东西,因为我读到应该有“\ page”书签 - 但是没有任何书签。什么是正确的方法?
注意:Word 2016,在.NET4.6.1下的VS2015中的Office工具
答案 0 :(得分:0)
你的getPage()非常接近解决方案。函数GoTo()在range.Start和range.End中返回页面的第一个字符索引,它不返回页码。因此,要获得页面的开始和结束,您需要运行GoTo两次。
不是最好的代码。我在C#中使用了一些Java代码约定,抱歉。
public Word.Range getPageByCharacterIndex(int characterIndex) {
var numberOfPages = (int)doc.Content.Information[Word.WdInformation.wdNumberOfPagesInDocument];
int lastCharInDoc = doc.Range(0).End;
Word.Range rangeStart = null; ;
Word.Range rangeEnd = null;
for (var p = 1; p <= numberOfPages; p++) {
object what = Word.WdGoToItem.wdGoToPage;
object which = Word.WdGoToDirection.wdGoToAbsolute;
object count = p;
rangeStart = doc.GoTo(ref what, ref which, ref count);
object countPlusOne = p + 1;
rangeEnd = doc.GoTo(ref what, ref which, ref countPlusOne);
if (rangeStart.Start <= characterIndex && characterIndex <= rangeEnd.End) {
Word.Range result = doc.Range(rangeStart.Start, rangeEnd.End);
return result;
}
}
// ¿is characterIndex in last page?
if (rangeEnd !=null & rangeEnd.Start <= characterIndex && characterIndex <= lastCharInDoc) {
Word.Range result = doc.Range(rangeEnd.Start, lastCharInDoc);
return result;
}
return null;
}