google-apps-script - 如何识别谷歌文档元素索引?
问题描述
我的编程技能很少,并且在雇用某人编写整个代码之前尝试做一个简单的概念证明。
我正在创建一个基于动态文档的输入表单结果。我想做两件我无法弄清楚的事情:
*编辑:表单数据进入谷歌表格。文档开头有一个表格,其中包含有关约会的信息。
我想在文档的约会信息表的特定单元格中获取单词并将其用作文件名。但是,我不知道如何识别该单元格文本的索引。
更改特定索引处标题的格式。我知道如何插入格式化文本、删除文本等。但不知道如何更改现有文本的格式。
我已经尝试了 .getParent()、.getChildIndex()、getNextSibling() 等的所有变体。
这是示例文档的链接
以下是我试图在标签“心理医生”旁边的单元格中找到索引的一些内容:
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);
}
我完全迷失在这里。我只想弄清楚如何告诉文件名代码以获取该位置的文本,因为文本会有所不同。
并且还可以更改特定位置的文本格式。
解决方案
一个帮助您从 Google Apps 脚本识别和编辑 Google Docs 中的表格和单元格的项目
该项目加载到您的侧边栏中并读取您的文档以查找表格。当它在显示所有行、单元格、行索引、单元格索引和单元格文本中找到表时。每个单元格都有一个文本框和一个保存按钮,可让您编辑单元格文本。但是您也可以只获取要在您自己的程序中使用的索引。
您必须创建一个名为“script”的 html 文件并将 script.html 代码加载到其中,其余代码可以进入 Code.gs 文件。如果您将项目与您正在工作的项目分开,那么您可以运行 onOpen ,它将创建一个不会打扰您项目中任何可能的菜单的菜单。
代码.gs:
函数“findingTableCells()”生成一个侧边栏,该边栏利用模板化方法,以便可以用一种不那么繁琐的方法开发 Javascript 函数,而不是试图将它们包含到一个字符串中,就好像它们被缩小了一样。它还包含工具提示,可帮助您识别各种索引和表格元素。
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;
}
样式.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;
脚本.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>
这是另一种方法,它将所有原始属性存储在客户端的全局数组中。
推荐阅读
- java - How do I count the occurence of an integer in a list and output it correctly?
- mongoose - 如何使用动态 ref 填充 Mongoose 子文档的虚拟数组?
- javascript - 如何限制在firebase中发布更改的订户端进行更改?
- linux - I am trying to create multi line variable in bash but facing error "unexpected EOF while looking for matching `)' "
- python - 我需要在 f.close() 之前使用 os.fsync() 吗?
- java - 使用 .matches 验证整数的问题
- iis - IIS 10 发送客户端 ip 作为响应头
- python - 循环遍历文件名列表并读取找不到文件名
- html - 仪表条可以升高 X 像素吗?
- javascript - How to filter external JSON data and get firstName using Javascript?