英文:
How to insert a cell value from another cell in an appendRow app script?
问题
I have translated the non-code part of your request. Here is the translated text:
"请原谅我,我是新手的应用脚本。提前谢谢。
我有一个带有一系列数据范围的提交按钮表单,目标在另一个工作表中。但是,我需要在appendRow
中插入日期、实体名称和办公室。
表单:
日期: | 实体名称: | 办公室: | ||
---|---|---|---|---|
ID | 库存编号 | 单位 | 描述 | 数量 |
1 | JTL-0005 | PC | BROOM,SOFT (TAMBO) | 1 |
输出(另一个工作表):
日期 | 实体名称 | 办公室 | ID | 库存编号 | 单位 | 描述 | 数量 |
---|---|---|---|---|---|---|---|
1 | JTL-0005 | PC | BROOM,SOFT (TAMBO) | 1 |
上述输出只附加了行。如何同时提交这些值?
提交按钮代码:
function btn_dataEntryPrevSubmit(){
var ui = SpreadsheetApp.getUi();
var response = ui.alert('你确定要继续吗?', ui.ButtonSet.YES_NO);
var result = ui.alert('请求已成功提交!', ui.ButtonSet.OK);
if (response == ui.Button.YES) {
const idCell = formDE.getRange("A4");
/*
let deDate = formDE.getRange("F4");
let deEntityName = formDE.getRange("F8");
let deOffice = formDE.getRange("F10");
*/
let dataEntryRange = formDE.getRange("D14:H");
let values = dataEntryRange.getValues();
for(let index = 0; index < values.length; index++){
db_RequisitionOffice.appendRow(values[index]);
const nextIDCell = formDE.getRange("A4");
const nextID = nextIDCell.getValue();
values[index].unshift(nextID);
idCell.setValue(nextID+1);
nextIDCell.setValue(nextID+1);
result;
}
//====================================
//删除数据录入表单中的值
//====================================
const dataEntryFieldRangeForDeletion = ["D14:H"];
dataEntryFieldRangeForDeletion.forEach(f => formDE.getRange(f).clearContent());
}
}
非常感谢你的帮助。"
英文:
forgive me, I am new to app script. Thank you in advance.
I have a submit button form with a range of data, the destination is in another sheet. However, I need to insert the date, entity name and office in the appendRow.
The form:
DATE : | Entity Name : | Office : | ||
---|---|---|---|---|
ID | STOCK NO. | UNIT | DESCRIPTION | QUANTITY |
1 | JTL-0005 | PC | BROOM, SOFT (TAMBO) | 1 |
Output (another sheet) :
DATE | ENTITY NAME | OFFICE | ID | STOCK NO. | UNIT | DESCRIPTION | QUANTITY |
---|---|---|---|---|---|---|---|
1 | JTL-0005 | PC | BROOM, SOFT (TAMBO) | 1 |
Above output only appends the row. How do I submit it with those values also.
Submit button code:
function btn_dataEntryPrevSubmit(){
var ui = SpreadsheetApp.getUi();
var response = ui.alert('Are you sure you want to continue?',
ui.ButtonSet.YES_NO);
var result = ui.alert('Successfully submitted request!',ui.ButtonSet.OK)
if (response == ui.Button.YES) {
const idCell = formDE.getRange("A4")
/*
let deDate = formDE.getRange("F4")
let deEntityName = formDE.getRange("F8")
let deOffice = formDE.getRange("F10")
*/
let dataEntryRange = formDE.getRange("D14:H")
let values = dataEntryRange.getValues()
for(let index = 0; index < values.length; index++){
db_RequisitionOffice.appendRow(values[index])
const nextIDCell = formDE.getRange("A4")
const nextID = nextIDCell.getValue()
values[index].unshift(nextID)
idCell.setValue(nextID+1)
nextIDCell.setValue(nextID+1)
result
}
//====================================
//DELETE VALUES IN DATA ENTRY FORM
//====================================
const dataEntryFieldRangeForDeletion = ["D14:H"]
dataEntryFieldRangeForDeletion.forEach(f => formDE.getRange(f).clearContent())
}
}
Thank you so much for your help.
答案1
得分: 2
替代方法
您可以使用map
和unshift
函数来调整脚本的输出以适应正确的列。
脚本:
您可以使用以下脚本作为编写自己代码的基础:
function adjustData() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form");
var ssOut = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Output");
var ssLastRow = ss.getLastRow();
var ssLastColumn = ss.getLastColumn()
var deDate = ss.getRange("F4").getValue();
var deEntityName = ss.getRange("F8").getValue();
var deOffice = ss.getRange("F10").getValue();
// 提取数据
var ssData = ss.getRange(14, 1, ssLastRow - 13, ssLastColumn).getValues();
ssData.map(x => {
x.unshift(deDate, deEntityName, deOffice); // 添加日期、实体名称和办公室到数据
ssOut.appendRow(x); // 传输数据
});
// 从表单中删除数据
ss.getRange(14, 1, ssLastRow - 13, ssLastColumn).clear();
}
结果
由于您没有分享示例电子表格,我根据您的帖子创建了一个测试用例。
1. 表单工作表 (输入)
2. 输出工作表 (输出)
3. 清除的表单工作表
参考:
英文:
Alternate Approach
You may use the map
and unshift
functions to adjust the output of the script to the proper columns.
Script:
You may use the following script as a basis for your own code:
function adjustData() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form");
var ssOut = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Output");
var ssLastRow = ss.getLastRow();
var ssLastColumn = ss.getLastColumn()
var deDate = ss.getRange("F4").getValue();
var deEntityName = ss.getRange("F8").getValue();
var deOffice = ss.getRange("F10").getValue();
//Extract Data
var ssData = ss.getRange(14, 1, ssLastRow - 13, ssLastColumn).getValues();
ssData.map(x => {
x.unshift(deDate, deEntityName, deOffice); //Add date, entity name, and office to data
ssOut.appendRow(x); //Transfer data
});
//delete data from form
ss.getRange(14, 1, ssLastRow - 13, ssLastColumn).clear();
}
Result
Since you have not shared a sample spreadsheet, I went ahead and created a test case based on your post.
1. Form Sheet (Input)
2. Output Sheet (Output)
3. Cleared Form Sheet
Reference:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论