找不到具有给定 ID 的项目
No item with Given ID can be found
在最近的 API 更新后 运行 我的脚本出现问题。我遇到了一些问题,我可以在附近的帮助下解决这些问题,但现在我遇到了通过 ID 获取问题。我不确定它是从哪里来的,因为我有一些东西是通过 ID 获得的。有什么帮助吗?这是我的代码,如果你想 运行 它和一张错误的图片。
如果你想 运行 它,你需要有一个名为 Templates 的文件夹和里面的一个文件。在文档内部只需要为电子表格中的 A 列设置 {A},它仍然会提示您该错误,谢谢!
function generateDocument(e) {
var template = DriveApp.getFileById(e.parameter.Templates);
Logger.log(template.getName());
var Sheet = SpreadsheetApp.getActiveSpreadsheet();
var row = Number(e.parameter.row) //+1; // Remove the // in this line next to the +1 to skip headers
Logger.log(row);
var currentFID = e.parameter.curFID;
Logger.log(currentFID);
var myDocID = template.makeCopy(Sheet.getRange('B' + row).getValue() + ' - ' + Sheet.getRange('E' + row).getValue() + ' - ' + Sheet.getRange('D' + row).getValue() + ' - ' + Sheet.getRange('X' + row).getValue()).getId();
var myDoc = DocumentApp.openById(myDocID);
var copyBody = myDoc.getActiveSection();
var Sheet = SpreadsheetApp.getActiveSpreadsheet();
//row--; // decrement row number to be in concordance with real row numbers in sheet
var myRow = SpreadsheetApp.getActiveSpreadsheet().getRange(row + ":" + row);
for (var i = 1; i < Sheet.getLastColumn() + 1; i++) {
var myCell = myRow.getCell(1, i);
copyBody.replaceText("{" + myCell.getA1Notation().replace(row, "") + "}", myCell.getValue());
}
myDoc.saveAndClose();
var destFolder = DriveApp.getFolderById(currentFID);
Logger.log(myDocID);
var doc = DriveApp.getFileById(myDocID); // get the document again but using DriveApp this time...
doc.addToFolder(destFolder); // add it to the desired folder
doc.removeFromFolder(DriveApp.getRootFolder()); // I did it step by step to be more easy to follow
var pdf = DriveApp.getFileById(myDocID).getAs("application/pdf");
destFolder.createFile(pdf); // this will create the pdf file in your folder
var app = UiApp.getActiveApplication();
app.close();
return app;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function getTemplates() {
var doc = SpreadsheetApp.getActiveSpreadsheet();
var app = UiApp.createApplication().setTitle('Generate from template');
// Create a grid with 3 text boxes and corresponding labels
var grid = app.createGrid(5, 2);
grid.setWidget(0, 0, app.createLabel('Template name:'));
var list = app.createListBox();
list.setName('Templates');
grid.setWidget(0, 1, list);
var folders = DriveApp.getFoldersByName("Templates");
while (folders.hasNext())
{
var folder = folders.next();
Logger.log(folder.getName());
var allMyFilesByType = folder.getFilesByType(MimeType.GOOGLE_DOCS)
};
while (allMyFilesByType.hasNext())
{
var file = allMyFilesByType.next();
list.addItem(file.getName());
Logger.log(file.getName());
};
grid.setWidget(1, 0, app.createLabel('Row:'));
var row = app.createTextBox().setName('row');
row.setValue(SpreadsheetApp.getActiveSpreadsheet().getActiveRange().getRow());
grid.setWidget(1, 1, row);
var curFN = app.createTextBox().setText('MyDrive/').setName('curFN').setId('curFN').setWidth('400');
var curFID = app.createTextBox().setText(DriveApp.getRootFolder().getId()).setName('curFID').setId('curFID').setVisible(false);
var listF = app.createListBox().setName('listF').setId('listF').addItem('Please Select Folder', 'x');
grid.setText(2, 0, 'Type Path:').setWidget(2, 1, curFN).setText(3, 0, 'OR').setText(4, 0, 'Choose Path:').setWidget(4, 1, listF).setWidget(3, 1, curFID);
var folders = DriveApp.getRootFolder().getFolders();
while (folders.hasNext()) {
var folder = folders.next();
listF.addItem(folder.getName(),folder.getId())
};
var handlerF = app.createServerHandler('folderSelect').addCallbackElement(grid);
listF.addChangeHandler(handlerF);
var panel = app.createVerticalPanel();
panel.add(grid);
var button = app.createButton('Submit');
var handler = app.createServerClickHandler('generateDocument');
handler.addCallbackElement(grid);
button.addClickHandler(handler);
// Add the button to the panel and the panel to the application, then display the application app in the Spreadsheet doc
panel.add(button);
app.add(panel);
doc.show(app);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function folderSelect(e)
{
var app = UiApp.getActiveApplication();
var currentFN = e.parameter.curFN;
var currentFID = e.parameter.listF;
Logger.log(currentFID);
var listF = app.getElementById('listF');
var curFN = app.getElementById('curFN');
var curFID = app.getElementById('curFID');
if (currentFID == 'x') {
currentFID = DriveApp.getRootFolder().getId();
curFN.setText('MyDrive/')
};
var startFolder = DriveApp.getFolderById(currentFID);
var folders = startFolder.getFolders();
listF.clear().addItem('No More Sub Folders!', 'x').addItem('Go back to Root', 'x');
if (folders.length > 0) {
listF.clear();
listF.addItem('Select Sub Folder', 'x')
};
while (folders.hasNext()) {
var folder = folders.next();
listF.addItem(folder.getName(),folder.getId())
};
curFN.setText(currentFN + DriveApp.getFolderById(currentFID).getName() + '/');
if (currentFID == DriveApp.getRootFolder().getId()) {
curFN.setText('MyDrive/')
};
curFID.setText(currentFID);
return app;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [{
name: "Export Row to Document",
functionName: "getTemplates"
}];
ss.addMenu("Generate Documents Here!", menuEntries);
}
这个语句有问题
var template = DriveApp.getFileById(e.parameter.Templates);
当您在记录器中看到 e.parameter.Templates 名称时,它将是一个文件名。但 DriveApp.getFileById 需要文件 ID。
在
中添加文件ID和文件名即可解决上述问题
list.addItem(file.getName(),file.getId());
getTemplates() 方法。
这是工作代码
function generateDocument(e)
{
//Logger.log(DriveApp.getFilesByName(e.parameter.Templates).);
//SpreadsheetApp.getUi().alert(e.parameter.Templates);
var template = DriveApp.getFileById(e.parameter.Templates);
//Logger.log(e.parameter.Templates.getId());
var Sheet = SpreadsheetApp.getActiveSpreadsheet();
var row = Number(e.parameter.row) //+1; // Remove the // in this line next to the +1 to skip headers
Logger.log(row);
var currentFID = e.parameter.listF;
Logger.log(currentFID);
var myDocID = template.makeCopy(Sheet.getRange('B' + row).getValue() + ' - ' + Sheet.getRange('E' + row).getValue() + ' - ' + Sheet.getRange('D' + row).getValue() + ' - ' + Sheet.getRange('X' + row).getValue()).getId();
var myDoc = DocumentApp.openById(myDocID);
var copyBody = myDoc.getActiveSection();
var Sheet = SpreadsheetApp.getActiveSpreadsheet();
//row--; // decrement row number to be in concordance with real row numbers in sheet
var myRow = SpreadsheetApp.getActiveSpreadsheet().getRange(row + ":" + row);
for (var i = 1; i < Sheet.getLastColumn() + 1; i++) {
var myCell = myRow.getCell(1, i);
copyBody.replaceText("{" + myCell.getA1Notation().replace(row, "") + "}", myCell.getValue());
}
myDoc.saveAndClose();
var destFolder = DriveApp.getFolderById(currentFID);
Logger.log(myDocID);
var doc = DriveApp.getFileById(myDocID); // get the document again but using DriveApp this time...
destFolder.addFile(doc);//add to filder
var pdf = DriveApp.getFileById(myDocID).getAs("application/pdf");
destFolder.createFile(pdf); // this will create the pdf file in your folder
var app = UiApp.getActiveApplication();
app.close();
return app;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function getTemplates()
{
var doc = SpreadsheetApp.getActiveSpreadsheet();
var app = UiApp.createApplication().setTitle('Generate from template');
// Create a grid with 3 text boxes and corresponding labels
var grid = app.createGrid(5, 2);
grid.setWidget(0, 0, app.createLabel('Template name:'));
var list = app.createListBox();
list.setName('Templates');
grid.setWidget(0, 1, list);
var folders = DriveApp.getFoldersByName("Templates");
while (folders.hasNext())
{
var folder = folders.next();
Logger.log(folder.getName());
var allMyFilesByType = folder.getFilesByType(MimeType.GOOGLE_DOCS)
};
while (allMyFilesByType.hasNext())
{
var file = allMyFilesByType.next();
list.addItem(file.getName(),file.getId());
Logger.log(file.getName());
};
grid.setWidget(1, 0, app.createLabel('Row:'));
var row = app.createTextBox().setName('row');
row.setValue(SpreadsheetApp.getActiveSpreadsheet().getActiveRange().getRow());
grid.setWidget(1, 1, row);
var curFN = app.createTextBox().setText('MyDrive/').setName('curFN').setId('curFN').setWidth('400');
var curFID = app.createTextBox().setText(DriveApp.getRootFolder().getId()).setName('curFID').setId('curFID').setVisible(false);
var listF = app.createListBox().setName('listF').setId('listF').addItem('Please Select Folder', 'x');
grid.setText(2, 0, 'Type Path:').setWidget(2, 1, curFN).setText(3, 0, 'OR').setText(4, 0, 'Choose Path:').setWidget(4, 1, listF).setWidget(3, 1, curFID);
var folders = DriveApp.getRootFolder().getFolders();
while (folders.hasNext()) {
var folder = folders.next();
listF.addItem(folder.getName(),folder.getId())
};
var handlerF = app.createServerHandler('folderSelect').addCallbackElement(grid);
listF.addChangeHandler(handlerF);
var panel = app.createVerticalPanel();
panel.add(grid);
var button = app.createButton('Submit');
var handler = app.createServerClickHandler('generateDocument');
handler.addCallbackElement(grid);
button.addClickHandler(handler);
// Add the button to the panel and the panel to the application, then display the application app in the Spreadsheet doc
panel.add(button);
app.add(panel);
doc.show(app);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function folderSelect(e)
{
var app = UiApp.getActiveApplication();
var currentFN = e.parameter.curFN;
var currentFID = e.parameter.listF;
Logger.log(currentFID);
var listF = app.getElementById('listF');
var curFN = app.getElementById('curFN');
var curFID = app.getElementById('curFID');
if (currentFID == 'x') {
currentFID = DriveApp.getRootFolder().getId();
curFN.setText('MyDrive/')
};
var startFolder = DriveApp.getFolderById(currentFID);
var folders = startFolder.getFolders();
listF.clear().addItem('No More Sub Folders!', 'x').addItem('Go back to Root', 'x');
if (folders.length > 0)
{
listF.clear();
listF.addItem('Select Sub Folder', 'x')
};
while (folders.hasNext())
{
var folder = folders.next();
listF.addItem(folder.getName(),folder.getId())
};
curFN.setText(currentFN + DriveApp.getFolderById(currentFID).getName() + '/');
if (currentFID == DriveApp.getRootFolder().getId()) {
curFN.setText('MyDrive/')
};
curFID.setText(currentFID);
return app;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function onOpen()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [{
name: "Export Row to Document",
functionName: "getTemplates"
}];
ss.addMenu("Generate Documents Here!", menuEntries);
}
在最近的 API 更新后 运行 我的脚本出现问题。我遇到了一些问题,我可以在附近的帮助下解决这些问题,但现在我遇到了通过 ID 获取问题。我不确定它是从哪里来的,因为我有一些东西是通过 ID 获得的。有什么帮助吗?这是我的代码,如果你想 运行 它和一张错误的图片。
如果你想 运行 它,你需要有一个名为 Templates 的文件夹和里面的一个文件。在文档内部只需要为电子表格中的 A 列设置 {A},它仍然会提示您该错误,谢谢!
function generateDocument(e) {
var template = DriveApp.getFileById(e.parameter.Templates);
Logger.log(template.getName());
var Sheet = SpreadsheetApp.getActiveSpreadsheet();
var row = Number(e.parameter.row) //+1; // Remove the // in this line next to the +1 to skip headers
Logger.log(row);
var currentFID = e.parameter.curFID;
Logger.log(currentFID);
var myDocID = template.makeCopy(Sheet.getRange('B' + row).getValue() + ' - ' + Sheet.getRange('E' + row).getValue() + ' - ' + Sheet.getRange('D' + row).getValue() + ' - ' + Sheet.getRange('X' + row).getValue()).getId();
var myDoc = DocumentApp.openById(myDocID);
var copyBody = myDoc.getActiveSection();
var Sheet = SpreadsheetApp.getActiveSpreadsheet();
//row--; // decrement row number to be in concordance with real row numbers in sheet
var myRow = SpreadsheetApp.getActiveSpreadsheet().getRange(row + ":" + row);
for (var i = 1; i < Sheet.getLastColumn() + 1; i++) {
var myCell = myRow.getCell(1, i);
copyBody.replaceText("{" + myCell.getA1Notation().replace(row, "") + "}", myCell.getValue());
}
myDoc.saveAndClose();
var destFolder = DriveApp.getFolderById(currentFID);
Logger.log(myDocID);
var doc = DriveApp.getFileById(myDocID); // get the document again but using DriveApp this time...
doc.addToFolder(destFolder); // add it to the desired folder
doc.removeFromFolder(DriveApp.getRootFolder()); // I did it step by step to be more easy to follow
var pdf = DriveApp.getFileById(myDocID).getAs("application/pdf");
destFolder.createFile(pdf); // this will create the pdf file in your folder
var app = UiApp.getActiveApplication();
app.close();
return app;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function getTemplates() {
var doc = SpreadsheetApp.getActiveSpreadsheet();
var app = UiApp.createApplication().setTitle('Generate from template');
// Create a grid with 3 text boxes and corresponding labels
var grid = app.createGrid(5, 2);
grid.setWidget(0, 0, app.createLabel('Template name:'));
var list = app.createListBox();
list.setName('Templates');
grid.setWidget(0, 1, list);
var folders = DriveApp.getFoldersByName("Templates");
while (folders.hasNext())
{
var folder = folders.next();
Logger.log(folder.getName());
var allMyFilesByType = folder.getFilesByType(MimeType.GOOGLE_DOCS)
};
while (allMyFilesByType.hasNext())
{
var file = allMyFilesByType.next();
list.addItem(file.getName());
Logger.log(file.getName());
};
grid.setWidget(1, 0, app.createLabel('Row:'));
var row = app.createTextBox().setName('row');
row.setValue(SpreadsheetApp.getActiveSpreadsheet().getActiveRange().getRow());
grid.setWidget(1, 1, row);
var curFN = app.createTextBox().setText('MyDrive/').setName('curFN').setId('curFN').setWidth('400');
var curFID = app.createTextBox().setText(DriveApp.getRootFolder().getId()).setName('curFID').setId('curFID').setVisible(false);
var listF = app.createListBox().setName('listF').setId('listF').addItem('Please Select Folder', 'x');
grid.setText(2, 0, 'Type Path:').setWidget(2, 1, curFN).setText(3, 0, 'OR').setText(4, 0, 'Choose Path:').setWidget(4, 1, listF).setWidget(3, 1, curFID);
var folders = DriveApp.getRootFolder().getFolders();
while (folders.hasNext()) {
var folder = folders.next();
listF.addItem(folder.getName(),folder.getId())
};
var handlerF = app.createServerHandler('folderSelect').addCallbackElement(grid);
listF.addChangeHandler(handlerF);
var panel = app.createVerticalPanel();
panel.add(grid);
var button = app.createButton('Submit');
var handler = app.createServerClickHandler('generateDocument');
handler.addCallbackElement(grid);
button.addClickHandler(handler);
// Add the button to the panel and the panel to the application, then display the application app in the Spreadsheet doc
panel.add(button);
app.add(panel);
doc.show(app);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function folderSelect(e)
{
var app = UiApp.getActiveApplication();
var currentFN = e.parameter.curFN;
var currentFID = e.parameter.listF;
Logger.log(currentFID);
var listF = app.getElementById('listF');
var curFN = app.getElementById('curFN');
var curFID = app.getElementById('curFID');
if (currentFID == 'x') {
currentFID = DriveApp.getRootFolder().getId();
curFN.setText('MyDrive/')
};
var startFolder = DriveApp.getFolderById(currentFID);
var folders = startFolder.getFolders();
listF.clear().addItem('No More Sub Folders!', 'x').addItem('Go back to Root', 'x');
if (folders.length > 0) {
listF.clear();
listF.addItem('Select Sub Folder', 'x')
};
while (folders.hasNext()) {
var folder = folders.next();
listF.addItem(folder.getName(),folder.getId())
};
curFN.setText(currentFN + DriveApp.getFolderById(currentFID).getName() + '/');
if (currentFID == DriveApp.getRootFolder().getId()) {
curFN.setText('MyDrive/')
};
curFID.setText(currentFID);
return app;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [{
name: "Export Row to Document",
functionName: "getTemplates"
}];
ss.addMenu("Generate Documents Here!", menuEntries);
}
这个语句有问题
var template = DriveApp.getFileById(e.parameter.Templates);
当您在记录器中看到 e.parameter.Templates 名称时,它将是一个文件名。但 DriveApp.getFileById 需要文件 ID。
在
中添加文件ID和文件名即可解决上述问题list.addItem(file.getName(),file.getId());
getTemplates() 方法。
这是工作代码
function generateDocument(e)
{
//Logger.log(DriveApp.getFilesByName(e.parameter.Templates).);
//SpreadsheetApp.getUi().alert(e.parameter.Templates);
var template = DriveApp.getFileById(e.parameter.Templates);
//Logger.log(e.parameter.Templates.getId());
var Sheet = SpreadsheetApp.getActiveSpreadsheet();
var row = Number(e.parameter.row) //+1; // Remove the // in this line next to the +1 to skip headers
Logger.log(row);
var currentFID = e.parameter.listF;
Logger.log(currentFID);
var myDocID = template.makeCopy(Sheet.getRange('B' + row).getValue() + ' - ' + Sheet.getRange('E' + row).getValue() + ' - ' + Sheet.getRange('D' + row).getValue() + ' - ' + Sheet.getRange('X' + row).getValue()).getId();
var myDoc = DocumentApp.openById(myDocID);
var copyBody = myDoc.getActiveSection();
var Sheet = SpreadsheetApp.getActiveSpreadsheet();
//row--; // decrement row number to be in concordance with real row numbers in sheet
var myRow = SpreadsheetApp.getActiveSpreadsheet().getRange(row + ":" + row);
for (var i = 1; i < Sheet.getLastColumn() + 1; i++) {
var myCell = myRow.getCell(1, i);
copyBody.replaceText("{" + myCell.getA1Notation().replace(row, "") + "}", myCell.getValue());
}
myDoc.saveAndClose();
var destFolder = DriveApp.getFolderById(currentFID);
Logger.log(myDocID);
var doc = DriveApp.getFileById(myDocID); // get the document again but using DriveApp this time...
destFolder.addFile(doc);//add to filder
var pdf = DriveApp.getFileById(myDocID).getAs("application/pdf");
destFolder.createFile(pdf); // this will create the pdf file in your folder
var app = UiApp.getActiveApplication();
app.close();
return app;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function getTemplates()
{
var doc = SpreadsheetApp.getActiveSpreadsheet();
var app = UiApp.createApplication().setTitle('Generate from template');
// Create a grid with 3 text boxes and corresponding labels
var grid = app.createGrid(5, 2);
grid.setWidget(0, 0, app.createLabel('Template name:'));
var list = app.createListBox();
list.setName('Templates');
grid.setWidget(0, 1, list);
var folders = DriveApp.getFoldersByName("Templates");
while (folders.hasNext())
{
var folder = folders.next();
Logger.log(folder.getName());
var allMyFilesByType = folder.getFilesByType(MimeType.GOOGLE_DOCS)
};
while (allMyFilesByType.hasNext())
{
var file = allMyFilesByType.next();
list.addItem(file.getName(),file.getId());
Logger.log(file.getName());
};
grid.setWidget(1, 0, app.createLabel('Row:'));
var row = app.createTextBox().setName('row');
row.setValue(SpreadsheetApp.getActiveSpreadsheet().getActiveRange().getRow());
grid.setWidget(1, 1, row);
var curFN = app.createTextBox().setText('MyDrive/').setName('curFN').setId('curFN').setWidth('400');
var curFID = app.createTextBox().setText(DriveApp.getRootFolder().getId()).setName('curFID').setId('curFID').setVisible(false);
var listF = app.createListBox().setName('listF').setId('listF').addItem('Please Select Folder', 'x');
grid.setText(2, 0, 'Type Path:').setWidget(2, 1, curFN).setText(3, 0, 'OR').setText(4, 0, 'Choose Path:').setWidget(4, 1, listF).setWidget(3, 1, curFID);
var folders = DriveApp.getRootFolder().getFolders();
while (folders.hasNext()) {
var folder = folders.next();
listF.addItem(folder.getName(),folder.getId())
};
var handlerF = app.createServerHandler('folderSelect').addCallbackElement(grid);
listF.addChangeHandler(handlerF);
var panel = app.createVerticalPanel();
panel.add(grid);
var button = app.createButton('Submit');
var handler = app.createServerClickHandler('generateDocument');
handler.addCallbackElement(grid);
button.addClickHandler(handler);
// Add the button to the panel and the panel to the application, then display the application app in the Spreadsheet doc
panel.add(button);
app.add(panel);
doc.show(app);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function folderSelect(e)
{
var app = UiApp.getActiveApplication();
var currentFN = e.parameter.curFN;
var currentFID = e.parameter.listF;
Logger.log(currentFID);
var listF = app.getElementById('listF');
var curFN = app.getElementById('curFN');
var curFID = app.getElementById('curFID');
if (currentFID == 'x') {
currentFID = DriveApp.getRootFolder().getId();
curFN.setText('MyDrive/')
};
var startFolder = DriveApp.getFolderById(currentFID);
var folders = startFolder.getFolders();
listF.clear().addItem('No More Sub Folders!', 'x').addItem('Go back to Root', 'x');
if (folders.length > 0)
{
listF.clear();
listF.addItem('Select Sub Folder', 'x')
};
while (folders.hasNext())
{
var folder = folders.next();
listF.addItem(folder.getName(),folder.getId())
};
curFN.setText(currentFN + DriveApp.getFolderById(currentFID).getName() + '/');
if (currentFID == DriveApp.getRootFolder().getId()) {
curFN.setText('MyDrive/')
};
curFID.setText(currentFID);
return app;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function onOpen()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [{
name: "Export Row to Document",
functionName: "getTemplates"
}];
ss.addMenu("Generate Documents Here!", menuEntries);
}