将 Google 文档 URL 粘贴到 Google 表格中并自动获取标题和字数
Paste Google Docs URLs into Google Sheets and automatically get title and word count
可能不太可能,但有谁知道可以与 Google 表格一起使用的任何公式或脚本,以便当您将 URL 的选择粘贴到 Google 工作表,它会自动获取每个 URL?
的标题和字数
试试这个:
function getIdTitleCount(url) {
var id=url.slice(url.indexOf('/d/')+3,url.indexOf('/edit'));
var title=DriveApp.getFileById(id).getName();
var count=DocumentApp.openById(id).getBody().getText().split(/\s/).length;//This probably could be a better regex but by my count it works pretty well for simple text.
Logger.log(id);
Logger.log(title);
Logger.log(count)
}
您所描述的先验可以使用自定义函数实现。它会在文档 API 中查询所需的元数据,然后 return 将其放入单元格中。
然而,这是不可能的,因为自定义函数 (1) 不允许访问 DocumentApp
等 API。尝试这样做时,他们将 return 出现如下错误:
You do not have permission to call DocumentApp.openByUrl.
Required permissions: https://www.googleapis.com/auth/documents (line 88).
备选
在整个 Sheet 文档中随意插入 =GETDOCWORDCOUNT()
和 =GETDOCTITLE()
函数。起初,他们将显示 #ERROR!
作为函数的结果,原因如上所述:
代码在打开 Sheets 文档时创建了一个 "Custom scripts" 菜单。将公式放入文档后,单击菜单,然后根据需要单击 select Run GETDOCWORDCOUNT
或 Run GETDOCTITLE
。 Sheet 中以前显示为 #ERROR!
的公式将被 运行 函数的结果替换。在此步骤中,执行 GETDOCWORDCOUNT
、GETDOCTITLE
函数时发现的任何错误也将显示给用户:
代码
var GETDOCWORDCOUNT_FUNCTION_REGEX = /=GETDOCWORDCOUNT\((.+)\)/;
var GETDOCTITLE_FUNCTION_REGEX = /=GETDOCTITLE\((.+)\)/;
var A1_CELL_REGEX = /^[A-Z]+[1-9][0-9]*$/;
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu("Custom scripts")
.addItem("Run GETDOCWORDCOUNT", "runGetDocWordCount")
.addItem("Run GETDOCTITLE", "runGetDocTitle")
.addToUi();
}
function runGetDocWordCount() {
var sh = SpreadsheetApp.getActiveSheet();
var finder = sh.createTextFinder("\=GETDOCWORDCOUNT\(.+)")
.matchCase(true)
.matchEntireCell(true)
.useRegularExpression(true)
.matchFormulaText(true);
var results = finder.findAll();
var errors = [];
for (var i=0; i<results.length; i++) {
var range = results[i];
var formula = range.getFormula();
var cell = formula.match(GETDOCWORDCOUNT_FUNCTION_REGEX)[1];
var url = sh.getRange(cell).getValue();
try {
range.setValue(GETDOCWORDCOUNT(url));
} catch(e) {
errors.push(range.getA1Notation() + ': ' + e.toString());
}
}
if (errors.length > 0) {
var ui = SpreadsheetApp.getUi();
var title = errors.length.toString() + ' errors found';
var prompt = errors.join('\n');
ui.alert(errors.length + ' errors found', prompt, ui.ButtonSet.OK);
}
}
function runGetDocTitle() {
var sh = SpreadsheetApp.getActiveSheet();
var finder = sh.createTextFinder("\=GETDOCTITLE\(.+)")
.matchCase(true)
.matchEntireCell(true)
.useRegularExpression(true)
.matchFormulaText(true);
var results = finder.findAll();
var errors = [];
for (var i=0; i<results.length; i++) {
var range = results[i];
var formula = range.getFormula();
var cell = formula.match(GETDOCTITLE_FUNCTION_REGEX)[1];
var url = sh.getRange(cell).getValue();
try {
range.setValue(GETDOCTITLE(url));
} catch(e) {
errors.push(range.getA1Notation() + ': ' + e.toString());
}
}
if (errors.length > 0) {
var ui = SpreadsheetApp.getUi();
var title = errors.length.toString() + ' errors found';
var prompt = errors.join('\n');
ui.alert(errors.length + ' errors found', prompt, ui.ButtonSet.OK);
}
}
function GETDOCWORDCOUNT(url) {
var doc = DocumentApp.openByUrl(url);
var text = doc.getBody().getText();
var words = text.split(/\S+/).length;
return words;
}
function GETDOCTITLE(url) {
var doc = DocumentApp.openByUrl(url);
return doc.getName();
}
function isA1Cell(val) {
if (typeof val != "string") return false;
return A1_CELL_REGEX.test(val);
}
演示
查看脚本的简短视频演示 here
可能不太可能,但有谁知道可以与 Google 表格一起使用的任何公式或脚本,以便当您将 URL 的选择粘贴到 Google 工作表,它会自动获取每个 URL?
的标题和字数试试这个:
function getIdTitleCount(url) {
var id=url.slice(url.indexOf('/d/')+3,url.indexOf('/edit'));
var title=DriveApp.getFileById(id).getName();
var count=DocumentApp.openById(id).getBody().getText().split(/\s/).length;//This probably could be a better regex but by my count it works pretty well for simple text.
Logger.log(id);
Logger.log(title);
Logger.log(count)
}
您所描述的先验可以使用自定义函数实现。它会在文档 API 中查询所需的元数据,然后 return 将其放入单元格中。
然而,这是不可能的,因为自定义函数 (1) 不允许访问 DocumentApp
等 API。尝试这样做时,他们将 return 出现如下错误:
You do not have permission to call DocumentApp.openByUrl.
Required permissions: https://www.googleapis.com/auth/documents (line 88).
备选
在整个 Sheet 文档中随意插入
=GETDOCWORDCOUNT()
和=GETDOCTITLE()
函数。起初,他们将显示#ERROR!
作为函数的结果,原因如上所述:代码在打开 Sheets 文档时创建了一个 "Custom scripts" 菜单。将公式放入文档后,单击菜单,然后根据需要单击 select
Run GETDOCWORDCOUNT
或Run GETDOCTITLE
。 Sheet 中以前显示为#ERROR!
的公式将被 运行 函数的结果替换。在此步骤中,执行GETDOCWORDCOUNT
、GETDOCTITLE
函数时发现的任何错误也将显示给用户:
代码
var GETDOCWORDCOUNT_FUNCTION_REGEX = /=GETDOCWORDCOUNT\((.+)\)/;
var GETDOCTITLE_FUNCTION_REGEX = /=GETDOCTITLE\((.+)\)/;
var A1_CELL_REGEX = /^[A-Z]+[1-9][0-9]*$/;
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu("Custom scripts")
.addItem("Run GETDOCWORDCOUNT", "runGetDocWordCount")
.addItem("Run GETDOCTITLE", "runGetDocTitle")
.addToUi();
}
function runGetDocWordCount() {
var sh = SpreadsheetApp.getActiveSheet();
var finder = sh.createTextFinder("\=GETDOCWORDCOUNT\(.+)")
.matchCase(true)
.matchEntireCell(true)
.useRegularExpression(true)
.matchFormulaText(true);
var results = finder.findAll();
var errors = [];
for (var i=0; i<results.length; i++) {
var range = results[i];
var formula = range.getFormula();
var cell = formula.match(GETDOCWORDCOUNT_FUNCTION_REGEX)[1];
var url = sh.getRange(cell).getValue();
try {
range.setValue(GETDOCWORDCOUNT(url));
} catch(e) {
errors.push(range.getA1Notation() + ': ' + e.toString());
}
}
if (errors.length > 0) {
var ui = SpreadsheetApp.getUi();
var title = errors.length.toString() + ' errors found';
var prompt = errors.join('\n');
ui.alert(errors.length + ' errors found', prompt, ui.ButtonSet.OK);
}
}
function runGetDocTitle() {
var sh = SpreadsheetApp.getActiveSheet();
var finder = sh.createTextFinder("\=GETDOCTITLE\(.+)")
.matchCase(true)
.matchEntireCell(true)
.useRegularExpression(true)
.matchFormulaText(true);
var results = finder.findAll();
var errors = [];
for (var i=0; i<results.length; i++) {
var range = results[i];
var formula = range.getFormula();
var cell = formula.match(GETDOCTITLE_FUNCTION_REGEX)[1];
var url = sh.getRange(cell).getValue();
try {
range.setValue(GETDOCTITLE(url));
} catch(e) {
errors.push(range.getA1Notation() + ': ' + e.toString());
}
}
if (errors.length > 0) {
var ui = SpreadsheetApp.getUi();
var title = errors.length.toString() + ' errors found';
var prompt = errors.join('\n');
ui.alert(errors.length + ' errors found', prompt, ui.ButtonSet.OK);
}
}
function GETDOCWORDCOUNT(url) {
var doc = DocumentApp.openByUrl(url);
var text = doc.getBody().getText();
var words = text.split(/\S+/).length;
return words;
}
function GETDOCTITLE(url) {
var doc = DocumentApp.openByUrl(url);
return doc.getName();
}
function isA1Cell(val) {
if (typeof val != "string") return false;
return A1_CELL_REGEX.test(val);
}
演示
查看脚本的简短视频演示 here