我正在处理GAS(谷歌APP脚本)中的代码,而且我已经在我的头上了。
这个附加到Google文档电子表格的代码应该从不同的标签中获取范围的集合,并将它们的值复制到新的CSV文件中。我已经整理了一个看起来很实用的脚本(基于我对GAS的有限经验)但是当我运行它时我得到了这个错误:
无法从此上下文调用DocumentApp.getUi()。
虽然我确定它充满了其他错误,但我不知道如何妥善解决。
这是我得到的:
// Function will run upon clicking assigned drawing object in spreadsheet
function exportData() {
// Prompts the user for the file name
var ui = DocumentApp.getUi();
var fileName = ui.prompt(
'Export Project Data',
'Save input values to new file?\n\nFile Name:',
ui.ButtonSet.OK_CANCEL);
// if user clicks 'yes' to save
if (response.getSelectedButton() == ui.Button.YES) {
// Check that the file name entered wasn't empty
if (fileName.length !== 0) {
// Add the ".csv" extension to the file name
fileName = fileName + ".csv";
// Convert the range data to CSV format using the function defined below
var csvFile = convertRangeToCsvFile_(fileName);
// Create a file in the Docs List with the given name and the CSV data
DriveApp.createFile(fileName, csvFile);
}
// If given file name is empty display message
else {
Browser.msgBox("Error: Please enter a CSV file name.");
}
}
// if user clicks 'no' to exit
if (response.getSelectedButton() == ui.Button.NO) {}
}
// Function will run when called upon by another function
function convertRangeToCsvFile_(csvFileName) {
// Define all the input ranges from the spreadsheet
var sheet1 = SpreadsheetApp.getActive().getSheetByName('Project Info');
var sheet2 = SpreadsheetApp.getActive().getSheetByName('Manufactured Materials');
var sheet3 = SpreadsheetApp.getActive().getSheetByName('Assembly Features');
var sheet4 = SpreadsheetApp.getActive().getSheetByName('Add-On Materials');
var sheet5 = SpreadsheetApp.getActive().getSheetByName('Decorative Pulls');
var sheet6 = SpreadsheetApp.getActive().getSheetByName('Häfele Hardware');
var sheet7 = SpreadsheetApp.getActive().getSheetByName('Häfele Lighting');
var sheet8 = SpreadsheetApp.getActive().getSheetByName('Rev-A-Shelf');
var sheet9 = SpreadsheetApp.getActive().getSheetByName('Sidelines');
var sheet10 = SpreadsheetApp.getActive().getSheetByName('Other Products');
var sheet11 = SpreadsheetApp.getActive().getSheetByName('Non-Stock');
var allRanges = [
sheet1.getRange("C4:C10"),
sheet1.getRange("E4:E10"),
sheet1.getRange("G4:G10"),
sheet1.getRange("C13:E19"),
sheet1.getRange("E21:E24"),
sheet1.getRange("A39:H50"),
sheet2.getRange("C3:I3"),
sheet2.getRange("C7:I8"),
sheet2.getRange("C11:I12"),
sheet2.getRange("C37:I37"),
sheet2.getRange("C40:I40"),
sheet2.getRange("C43:I43"),
sheet2.getRange("C46:I48"),
sheet2.getRange("C51:I53"),
sheet2.getRange("C55:I57"),
sheet2.getRange("C59:I61"),
sheet2.getRange("C63:I63"),
sheet2.getRange("C67:I67"),
sheet2.getRange("B70:I74"),
sheet3.getRange("C3:I7"),
sheet3.getRange("C12:I13"),
sheet3.getRange("C16:I18"),
sheet3.getRange("C21:I23"),
sheet3.getRange("C25:I27"),
sheet3.getRange("C31:I38"),
sheet3.getRange("C41:I41"),
sheet4.getRange("C3:I4"),
sheet4.getRange("C6:I6"),
sheet4.getRange("C32:I37"),
sheet4.getRange("C39:I40"),
sheet4.getRange("C43:I48"),
sheet4.getRange("C52:I54"),
sheet4.getRange("C57:I57"),
sheet4.getRange("C59:I61"),
sheet4.getRange("C63:I65"),
sheet4.getRange("C67:I68"),
sheet4.getRange("C72:I72"),
sheet4.getRange("C74:I77"),
sheet4.getRange("C81:I83"),
sheet4.getRange("C86:I88"),
sheet4.getRange("C91:I95"),
sheet5.getRange("A3:G50"),
sheet6.getRange("A3:G150"),
sheet7.getRange("A3:G120"),
sheet8.getRange("A3:G50"),
sheet9.getRange("A3:G50"),
sheet10.getRange("A3:G30"),
sheet11.getRange("B5:K14"),
sheet11.getRange("B17:K26"),
sheet11.getRange("B29:K38"),
sheet11.getRange("B41:K50"),
sheet11.getRange("B53:K62"),
sheet11.getRange("B65:K74"),
sheet11.getRange("B77:K86")
]
// Get the values from the input ranges in the spreadsheet
try {
var csvFile = undefined;
var data = sheet1.getValues(allRanges);
// Loop through the data in the range and build a string with the CSV data
if (data.length > 1) {
var csv = "";
for (var row = 0; row < data.length; row++) {
for (var col = 0; col < data[row].length; col++) {
if (data[row][col].toString().indexOf(",") != -1) {
data[row][col] = "\"" + data[row][col] + "\"";
}
}
// Join each row's columns
// Add a carriage return to end of each row, except for the last one
if (row < data.length-1) {
csv += data[row].join(",") + "\r\n";
}
else {
csv += data[row];
}
}
csvFile = csv;
}
return csvFile;
}
catch(err) {
Logger.log(err);
Browser.msgBox(err);
}
}