如何识别 google 文档元素索引?
How to identify a google docs element index?
我的编程技能很少,在雇用某人编写整个代码之前,我正在尝试做一个简单的概念证明。
我正在创建一个基于输入表单结果的动态文档。我想做两件我想不通的事:
*edit:表单数据进入 google 工作表。文档开头有一个 table,其中包含有关约会的信息。
我想在文档的约会信息 table 的特定单元格中使用单词并将其用作文件名。但是,我不知道如何识别该单元格文本的索引。
更改特定索引处标题的格式。我知道如何插入格式化文本、删除文本等。但不知道如何更改现有文本的格式。
我已经尝试了 .getParent()、.getChildIndex()、getNextSibling() 等的所有变体
这里是link的例子document
以下是我尝试在标签旁边的单元格中找到索引的一些内容 "Psychometrist":
function findElement() {
var foundTag = body.findText('Psychometrist');
if (foundTag != null) {
var tagElement = foundTag.getElement();
var parent = tagElement.getParent();
var parentLoc = parent.getParent().getChildIndex(parent);
var parentTwo = parent.getParent();
var parentTwoLoc = tagElement.getPreviousSibling();
var parentTwoLocA = parentTwoLoc.
}
Logger.log(tagElement);
Logger.log(parent);
Logger.log(parentLoc);
Logger.log(parentTwo);
Logger.log(parentTwoLoc);
}
我完全迷失在这里。我只是想弄清楚如何告诉 file-name 代码获取该位置的文本,因为文本会有所不同。
并更改特定位置的文本格式。
帮助您识别和编辑来自 Google Apps 脚本的 Google 文档中的表格和单元格的项目
此项目加载到您的边栏并阅读您的文档以查找 tables。当它在显示所有行、单元格、行索引、单元格索引和单元格文本时发现 tables。每个单元格都有一个文本框和一个允许您编辑单元格文本的保存按钮。但您也可以只获取索引以在您自己的程序中使用。
您必须创建一个名为 'script' 的 html 文件并将 script.html 代码加载到其中,其余代码可以放入 Code.gs 文件.如果你从你正在工作的项目中创建单独的项目,那么你可以 运行 onOpen 它将创建一个菜单,不会打扰你项目中的任何可能的菜单。
Code.gs:
函数 "findingTableCells()" 生成一个侧边栏,它使用模板化方法,以便 Javascript 函数可以用更简单的方法开发,而不是试图将它们包含到字符串中,就好像它们被缩小了一样。它还包含工具提示,可帮助您识别各种索引和 table 元素。
function onOpen() {
DocumentApp.getUi().createMenu('Table Menu')
.addItem('Find Table Cells', 'findingTableCells')
.addToUi();
}
function findingTableCells() {
var d=DocumentApp.getActiveDocument();
var body=d.getBody();
var numChildren=body.getNumChildren();
var html='<html><head>';
html+='<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>';
html+='<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">';
html+='<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>';
html+='<style>';
html+='body{font-size:14px;font-family:calibri;}';
html+='td,th{border:1px solid black;}';
html+='.tblcell input[type="text"]{background-color:#ffff00;margin:2px 1px;}';
html+='.tblcell input{margin:1px;}';
html+='.tblcell a{text-decoration:none;}';
html+='.childhdr{background-color:#00ffff;}';
html+='.childhdr a{text-decoration:none;}';
html+='</style></head><body><table>';
for(var i=0;i<numChildren;i++) {
var child=body.getChild(i);
if(child.getType()==DocumentApp.ElementType.TABLE) {
html+=Utilities.formatString('<span class="childhdr">Child[<a href="#" title="Child Index">%s</a>]:</span><br />',i);
var numRows=child.asTable().getNumRows();
for(var ri=0;ri<numRows;ri++) {
var numCells=child.asTable().getRow(ri).getNumCells();
for(var ci=0;ci<numCells;ci++) {
var cellText=child.asTable().getRow(ri).getCell(ci).editAsText().getText();
html+=Utilities.formatString('<span class="tblcell">R:[<a href="#" title="Row Index">%s</a>] C:[<a href="#" title="Cell Index">%s</a>]:<input type="text" value="%s" id="%s-%s-%s" size="10" title="Editable Cell Text"/>',ri,ci,cellText,i,ri,ci);
html+=Utilities.formatString('<input type="button" value="Save" onClick="saveCellText({child:%s,row:%s,cell:%s})" title="Saves Text using slightly different text style."/>', i,ri,ci);
html+=Utilities.formatString('<input type="button" value="Bold" onClick="setCellTextStyleBold({child:%s,row:%s,cell:%s})" title="Bolds & Enlarges Text"/></span><br />', i,ri,ci);
}
}
}
}
html+='<?!= include("script") ?>'
html+='</table></body></html>';
var ui=HtmlService.createTemplate(html).evaluate().setTitle('Finding Table Cells');
DocumentApp.getUi().showSidebar(ui);
}
function include(filename) {
return HtmlService.createHtmlOutputFromFile(filename).getContent();
}
function saveCellText(cObj) {
var doc=DocumentApp.getActiveDocument();
var body=doc.getBody();
body.getChild(cObj.child).asTable().getRow(cObj.row).getCell(cObj.cell).editAsText().setText(cObj.text).setAttributes(Normal1);
return cObj;
}
function setCellTextStyleBold(cObj) {
var doc=DocumentApp.getActiveDocument();
var body=doc.getBody();
body.getChild(cObj.child).asTable().getRow(cObj.row).getCell(cObj.cell).editAsText().setAttributes(Bold1);
return cObj;
}
style.gs:
style.gs 文件只有几个简单的样式对象,用于演示如何以编程方式更改样式。
var Bold1={};
Bold1[DocumentApp.Attribute.BOLD]=true;
Bold1[DocumentApp.Attribute.FONT_SIZE]=14;
var Normal1={};
Normal1[DocumentApp.Attribute.BOLD]=false;
Normal1[DocumentApp.Attribute.FONT_SIZE]=10;
script.html
这里只有两个功能,一个以一种样式保存您在单元格中所做的任何编辑。另一个只是加粗并放大文本。他们都使用 google.script.run 执行客户端到服务器的通信。
<script>
function saveCellText(cObj) {
var id='#' + cObj.child + '-' + cObj.row + '-' + cObj.cell;
$(id).css('background-color','#ffffff');
var txt=$(id).val();
cObj['text']=txt;
google.script.run
.withSuccessHandler(function(cObj) {
var id1='#' + cObj.child + '-' + cObj.row + '-' + cObj.cell;
$(id1).css('background-color','#ffff00');
})
.saveCellText(cObj);
}
function setCellTextStyleBold(cObj) {
var id='#' + cObj.child + '-' + cObj.row + '-' + cObj.cell;
$(id).css('background-color','#ffffff');
var txt=$(id).val();
cObj['text']=txt;
google.script.run
.withSuccessHandler(function(cObj) {
var id1='#' + cObj.child + '-' + cObj.row + '-' + cObj.cell;
$(id1).css('background-color','#ffff00');
})
.setCellTextStyleBold(cObj);
}
</script>
Here's another approach 将所有原始属性存储在客户端的全局数组中。
我的编程技能很少,在雇用某人编写整个代码之前,我正在尝试做一个简单的概念证明。
我正在创建一个基于输入表单结果的动态文档。我想做两件我想不通的事:
*edit:表单数据进入 google 工作表。文档开头有一个 table,其中包含有关约会的信息。
我想在文档的约会信息 table 的特定单元格中使用单词并将其用作文件名。但是,我不知道如何识别该单元格文本的索引。
更改特定索引处标题的格式。我知道如何插入格式化文本、删除文本等。但不知道如何更改现有文本的格式。
我已经尝试了 .getParent()、.getChildIndex()、getNextSibling() 等的所有变体
这里是link的例子document
以下是我尝试在标签旁边的单元格中找到索引的一些内容 "Psychometrist":
function findElement() {
var foundTag = body.findText('Psychometrist');
if (foundTag != null) {
var tagElement = foundTag.getElement();
var parent = tagElement.getParent();
var parentLoc = parent.getParent().getChildIndex(parent);
var parentTwo = parent.getParent();
var parentTwoLoc = tagElement.getPreviousSibling();
var parentTwoLocA = parentTwoLoc.
}
Logger.log(tagElement);
Logger.log(parent);
Logger.log(parentLoc);
Logger.log(parentTwo);
Logger.log(parentTwoLoc);
}
我完全迷失在这里。我只是想弄清楚如何告诉 file-name 代码获取该位置的文本,因为文本会有所不同。
并更改特定位置的文本格式。
帮助您识别和编辑来自 Google Apps 脚本的 Google 文档中的表格和单元格的项目
此项目加载到您的边栏并阅读您的文档以查找 tables。当它在显示所有行、单元格、行索引、单元格索引和单元格文本时发现 tables。每个单元格都有一个文本框和一个允许您编辑单元格文本的保存按钮。但您也可以只获取索引以在您自己的程序中使用。
您必须创建一个名为 'script' 的 html 文件并将 script.html 代码加载到其中,其余代码可以放入 Code.gs 文件.如果你从你正在工作的项目中创建单独的项目,那么你可以 运行 onOpen 它将创建一个菜单,不会打扰你项目中的任何可能的菜单。
Code.gs:
函数 "findingTableCells()" 生成一个侧边栏,它使用模板化方法,以便 Javascript 函数可以用更简单的方法开发,而不是试图将它们包含到字符串中,就好像它们被缩小了一样。它还包含工具提示,可帮助您识别各种索引和 table 元素。
function onOpen() {
DocumentApp.getUi().createMenu('Table Menu')
.addItem('Find Table Cells', 'findingTableCells')
.addToUi();
}
function findingTableCells() {
var d=DocumentApp.getActiveDocument();
var body=d.getBody();
var numChildren=body.getNumChildren();
var html='<html><head>';
html+='<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>';
html+='<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">';
html+='<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>';
html+='<style>';
html+='body{font-size:14px;font-family:calibri;}';
html+='td,th{border:1px solid black;}';
html+='.tblcell input[type="text"]{background-color:#ffff00;margin:2px 1px;}';
html+='.tblcell input{margin:1px;}';
html+='.tblcell a{text-decoration:none;}';
html+='.childhdr{background-color:#00ffff;}';
html+='.childhdr a{text-decoration:none;}';
html+='</style></head><body><table>';
for(var i=0;i<numChildren;i++) {
var child=body.getChild(i);
if(child.getType()==DocumentApp.ElementType.TABLE) {
html+=Utilities.formatString('<span class="childhdr">Child[<a href="#" title="Child Index">%s</a>]:</span><br />',i);
var numRows=child.asTable().getNumRows();
for(var ri=0;ri<numRows;ri++) {
var numCells=child.asTable().getRow(ri).getNumCells();
for(var ci=0;ci<numCells;ci++) {
var cellText=child.asTable().getRow(ri).getCell(ci).editAsText().getText();
html+=Utilities.formatString('<span class="tblcell">R:[<a href="#" title="Row Index">%s</a>] C:[<a href="#" title="Cell Index">%s</a>]:<input type="text" value="%s" id="%s-%s-%s" size="10" title="Editable Cell Text"/>',ri,ci,cellText,i,ri,ci);
html+=Utilities.formatString('<input type="button" value="Save" onClick="saveCellText({child:%s,row:%s,cell:%s})" title="Saves Text using slightly different text style."/>', i,ri,ci);
html+=Utilities.formatString('<input type="button" value="Bold" onClick="setCellTextStyleBold({child:%s,row:%s,cell:%s})" title="Bolds & Enlarges Text"/></span><br />', i,ri,ci);
}
}
}
}
html+='<?!= include("script") ?>'
html+='</table></body></html>';
var ui=HtmlService.createTemplate(html).evaluate().setTitle('Finding Table Cells');
DocumentApp.getUi().showSidebar(ui);
}
function include(filename) {
return HtmlService.createHtmlOutputFromFile(filename).getContent();
}
function saveCellText(cObj) {
var doc=DocumentApp.getActiveDocument();
var body=doc.getBody();
body.getChild(cObj.child).asTable().getRow(cObj.row).getCell(cObj.cell).editAsText().setText(cObj.text).setAttributes(Normal1);
return cObj;
}
function setCellTextStyleBold(cObj) {
var doc=DocumentApp.getActiveDocument();
var body=doc.getBody();
body.getChild(cObj.child).asTable().getRow(cObj.row).getCell(cObj.cell).editAsText().setAttributes(Bold1);
return cObj;
}
style.gs:
style.gs 文件只有几个简单的样式对象,用于演示如何以编程方式更改样式。
var Bold1={};
Bold1[DocumentApp.Attribute.BOLD]=true;
Bold1[DocumentApp.Attribute.FONT_SIZE]=14;
var Normal1={};
Normal1[DocumentApp.Attribute.BOLD]=false;
Normal1[DocumentApp.Attribute.FONT_SIZE]=10;
script.html
这里只有两个功能,一个以一种样式保存您在单元格中所做的任何编辑。另一个只是加粗并放大文本。他们都使用 google.script.run 执行客户端到服务器的通信。
<script>
function saveCellText(cObj) {
var id='#' + cObj.child + '-' + cObj.row + '-' + cObj.cell;
$(id).css('background-color','#ffffff');
var txt=$(id).val();
cObj['text']=txt;
google.script.run
.withSuccessHandler(function(cObj) {
var id1='#' + cObj.child + '-' + cObj.row + '-' + cObj.cell;
$(id1).css('background-color','#ffff00');
})
.saveCellText(cObj);
}
function setCellTextStyleBold(cObj) {
var id='#' + cObj.child + '-' + cObj.row + '-' + cObj.cell;
$(id).css('background-color','#ffffff');
var txt=$(id).val();
cObj['text']=txt;
google.script.run
.withSuccessHandler(function(cObj) {
var id1='#' + cObj.child + '-' + cObj.row + '-' + cObj.cell;
$(id1).css('background-color','#ffff00');
})
.setCellTextStyleBold(cObj);
}
</script>
Here's another approach 将所有原始属性存储在客户端的全局数组中。