如何使用 Google Apps 脚本通过函数删除 google 文档中的所有内容?
How do I use Google Apps Script to delete everything in a google document via a function?
我正在尝试在 google 文档上创建一个函数来清除文档,(删除文档中的所有内容,这是我目前所做的:
function onOpen() {
let ui = DocumentApp.getUi();
ui.createMenu('Extras')
.addItem('Purge Document', 'myFunction')
.addToUi();
};
function myFunction() {
var ui = DocumentApp.getUi();
var result = ui.alert(
'Purge?',
'Are you sure you want to purge the document? It will delete EVERYTHING. This is irreversible.',
ui.ButtonSet.YES_NO);
// responses
if (result == ui.Button.YES) {
// Clicked Yes ^^^
ui.alert('Purging...');
purge()
} else {
// User clicked No vvv
}
}
function purge(doc){
[heres where I need help]
}
有人可以帮助我吗?
谢谢
可能像这样的东西会起作用:
function purge() {
var doc = DocumentApp.getActiveDocument()
try { doc.getBody().clear() } catch(e) {}
try { doc.getFooter().clear() } catch(e) {}
try { doc.getHeader().clear() } catch(e) {}
}
电子表格的代码,以防万一:
function purge() {
SpreadsheetApp.getActiveSpreadsheet().getSheets().forEach(s => s.clear());
}
我正在尝试在 google 文档上创建一个函数来清除文档,(删除文档中的所有内容,这是我目前所做的:
function onOpen() {
let ui = DocumentApp.getUi();
ui.createMenu('Extras')
.addItem('Purge Document', 'myFunction')
.addToUi();
};
function myFunction() {
var ui = DocumentApp.getUi();
var result = ui.alert(
'Purge?',
'Are you sure you want to purge the document? It will delete EVERYTHING. This is irreversible.',
ui.ButtonSet.YES_NO);
// responses
if (result == ui.Button.YES) {
// Clicked Yes ^^^
ui.alert('Purging...');
purge()
} else {
// User clicked No vvv
}
}
function purge(doc){
[heres where I need help]
}
有人可以帮助我吗? 谢谢
可能像这样的东西会起作用:
function purge() {
var doc = DocumentApp.getActiveDocument()
try { doc.getBody().clear() } catch(e) {}
try { doc.getFooter().clear() } catch(e) {}
try { doc.getHeader().clear() } catch(e) {}
}
电子表格的代码,以防万一:
function purge() {
SpreadsheetApp.getActiveSpreadsheet().getSheets().forEach(s => s.clear());
}