gpt4 book ai didi

arrays - 在 Apps 脚本中从数组创建新数组并将值推送到不同工作表的最佳方法是什么?

转载 作者:行者123 更新时间:2023-12-02 11:38:29 25 4
gpt4 key购买 nike

这里是应用程序脚本新手,因此我们将不胜感激您提供的任何帮助/指导!

我有一个现有的 Apps Script Google Sheets内这是迭代行,并将每行单独推送到一个新工作表。我正在尝试修改它以获取整个数组,并一次将其全部推送,以使其更加高效,因为它得到了更多的利用。

这是我尝试使用的数据的示例: enter image description here

有了这么多代码,我可以成功过滤到仅具有值的行,迭代它们并确保填写所有必需的值。

function userSubmitTest() {
var ss = SpreadsheetApp.openById('1ozOeBPLzouZTEm0SECW0JZwfxHMcr3dr-augiTra6tY');
var dataSheet = ss.getSheetByName('Data Entry');
var downloadSheet = ss.getSheetByName('ExportForDownload');
var historicalSheet = ss.getSheetByName('ExportHistorical');
var spreadsheetTimeZone = SpreadsheetApp.getActive().getSpreadsheetTimeZone();
var submittedDate = Utilities.formatDate(new Date(), spreadsheetTimeZone, "MM/dd/yyyy' 'HH:mm:ss");

// Data Sheet Details
var downloadNumColumns = downloadSheet.getLastColumn();
var downloadIndex = downloadSheet.getLastRow();

//Historical Sheet Details
var historicalNumColumns = historicalSheet.getLastColumn();
var historicalIndex = historicalSheet.getLastRow();

// Initial Sheet Values
var newLastColumn = dataSheet.getLastColumn();
var newLastRow = dataSheet.getLastRow();
var dataToProcess = dataSheet.getRange(2, 1, newLastRow - 1, newLastColumn).getValues().filter(function(row) {
return row[0]
})

// Get Spreadsheet UI
var ui = SpreadsheetApp.getUi();
var dLen = dataToProcess.length;
var row = 2;

// Loop through processing lines to make sure A:D are filled out for each row
for (var i = 0; i < dLen; ++i) {
var bolCheck = dataToProcess[i][0];
var sapCheck = dataToProcess[i][1];
var notesCheck = dataToProcess[i][2];
var reasonCheck = dataToProcess[i][3];

if (bolCheck == "") { // Check all processing lines to make sure delivery docs are entered
var noBOL = ui.alert(
'Warning!',
'Please ensure all lines have a Delivery Document number in A2:A, or clear them, before submitting.',
ui.ButtonSet.OK);
return
} else if (sapCheck == "") { // Check all processing lines to make sure order numbers are entered
var noSAP = ui.alert(
'Warning!',
'Please ensure all lines have a SAP/ECC Order Number number in B2:B, or clear them, before submitting.',
ui.ButtonSet.OK);
return
} else if (notesCheck == "") { // Check all processing lines to make sure notes are entered
var noNotes = ui.alert(
'Warning!',
'Please ensure all lines have notes listed in C2:C, or clear them before submitting.',
ui.ButtonSet.OK);
return
} else if (reasonCheck == "") { // Check all processing lines to make sure reasons are entered
var noNotes = ui.alert(
'Warning!',
'Please ensure all lines have a reason selected in D2:D, or clear them before submitting.',
ui.ButtonSet.OK);
return
}

}

我可以看出大部分工作正在发挥作用,因为在调试中,我可以看到以下二维数组: enter image description here

但是,我需要获取该数组,并在序数位置之间插入一些值,并在下载过程的末尾添加一些新值。当迭代行时,这非常容易,因为我基本上会说“对于这一行,这个值是什么?”,然后创建一个包含我想要推送的所有值的新行“数组”。但是,如何将 A:A 列中的所有值放入一行,并开始根据这些序数位置和我尝试插入的其他一些文本/日期构建一个新数组?数组构建完成后,同时设置所有值,而不是逐行迭代。

这是我当前在迭代行时使用的:

for (var j = 0; j < dLen; ++j) {
var bolCheck = dataToProcess[j][0];
var sapCheck = dataToProcess[j][1];
var notesCheck = dataToProcess[j][2];
var reasonCheck = dataToProcess[j][3];
var rdd = dataToProcess[j][4];
if (rdd) {
rdd = Utilities.formatDate(rdd, spreadsheetTimeZone, "MM/dd/yyyy");
}
var submittedBy = dataToProcess[j][5].toString();
var submittedOn = dataToProcess[j][6];
if (submittedOn) {
submittedOn = Utilities.formatDate(submittedOn, spreadsheetTimeZone, "MM/dd/yyyy' 'HH:mm:ss");
}
var array = [];
array.push(bolCheck, "Company Submitted", sapCheck, reasonCheck, rdd, "Company Order Desk", "1-800-999-9999", "", notesCheck, submittedBy, submittedBy, submittedOn, submittedDate, "Company Submitted " + submittedDate, "Company Submitted " + submittedDate, submittedDate);

downloadSheet.insertRowsAfter(downloadIndex, 1).getRange(downloadIndex + 1, 1, 1, historicalNumColumns).setValues([array]);
historicalSheet.insertRowsAfter(historicalIndex, 1).getRange(historicalIndex + 1, 1, 1, historicalNumColumns).setValues([array]);

如果有任何不清楚的地方,请务必告诉我,以便我可以添加更多背景信息。任何知道链接的人都可以编辑 Google 表格,因此您应该能够看到我正在尝试的内容。

感谢您的浏览!

最佳答案

我想你已经差不多明白了。最后要做的事情是将单行数组添加到最后一个函数中的另一个“所有行”数组中。像这样:

var allRows = []; //define outside your loop
for (var j = 0; j < dLen; ++j) {
var bolCheck = dataToProcess[j][0];

//... same code you already have ...

array.push(bolCheck, "Company Submitted", sapCheck, reasonCheck, rdd, "Company Order Desk", "1-800-999-9999", "", notesCheck, submittedBy, submittedBy, submittedOn, submittedDate, "Company Submitted " + submittedDate, "Company Submitted " + submittedDate, submittedDate);

allRows.push(array);
} //close your for loop

downloadSheet
.insertRowsAfter(downloadIndex, allRows.length)
.getRange(downloadIndex + 1, 1, allRows.length, historicalNumColumns)
.setValues(allRows);
historicalSheet
.insertRowsAfter(historicalIndex, allRows.length)
.getRange(historicalIndex + 1, 1, allRows.legnth, historicalNumColumns)
.setValues(allRows);

ps:我还没有测试过这个,因为这个片段不完整

关于arrays - 在 Apps 脚本中从数组创建新数组并将值推送到不同工作表的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52411232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com