英文:
Copy Row Contents but Only Certain Cells and add Timestamp
问题
以下是您要翻译的内容:
function onEdit(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Main');
var date = new Date();
var rowIdx = sheet.getActiveRange().getRowIndex();
var rowValues = sheet.getRange(rowIdx,1,1,sheet.getLastRow()).getValues();
Logger.log(rowValues);
var destValues = [];
destValues.push(rowValues[0][0]);// copy data from col A to col A
destValues.push(rowValues[0][2]);// copy data from col C to col B
destValues.push(rowValues[0][1]);// copy data from col B to col C
destValues.push(rowValues[0][3]);// copy data from col D to col D
var dest = SpreadsheetApp.getActive().getSheetByName('Updates');
dest.getRange(dest.getLastRow()+1,1,1,destValues.length).setValues([destValues]);
}
请注意,我已经删除了代码部分以进行翻译,只返回了已翻译的部分。
英文:
I would like to have it that when a row is edited that it only copies the name phone and adds a timestamp of the edit on another tab in Google Sheets.
function onEdit(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Main');
var date = new Date();
var rowIdx = sheet.getActiveRange().getRowIndex();
var rowValues = sheet.getRange(rowIdx,1,1,sheet.getLastRow()).getValues();
Logger.log(rowValues);
var destValues = [];
destValues.push(rowValues[0][0]);// copy data from col A to col A
destValues.push(rowValues[0][2]);// copy data from col C to col B
destValues.push(rowValues[0][1]);// copy data from col B to col C
destValues.push(rowValues[0][3]);// copy data from col D to col D
var dest = SpreadsheetApp.getActive().getSheetByName('Updates');
dest.getRange(dest.getLastRow()+1,1,1,destValues.length).setValues([destValues]);
}
The above is working but i cannot figure out how to get the timestamp to show.
The outcome should display the roadname, phone and then C:C should have timestamp of change made.
I have updated the below sheet to show expected outcome
How I want it to look.
Result Image
Data source
Source Data
答案1
得分: 0
你的书面解释与示例输出不一致,而示例输出与你的脚本也不一致。在destValues.push()
中,你将4个值推送到数组中(包括"Phone Number"、"Date Contacted"和"Clan Name"),但你的期望结果只有三列(不包括"Date Contacted"和"Phone Number"),但"Phone Number"实际上在"Clan Name"列中。
替换后的代码:
var date = new Date();
destValues.push(rowValues[0][0]);// 将数据从A列复制到A列
destValues.push(rowValues[0][2]);// 将数据从C列复制到B列
destValues.push(date);// 将日期数据复制到C列
var dest = SpreadsheetApp.getActive().getSheetByName('Updates');
dest.getRange(dest.getLastRow()+1,1,1,destValues.length).setValues([destValues]);
希望这有所帮助。
英文:
Your written explanation is inconsistent with your sample output which is inconsistent with your script. destValues.push()
-you are pushing 4 (four) values onto the array (including "Phone Number", "Date Contacted" and "Clan Name") but your expected outcome only has three columns (excluding "Date Contacted" and "Phone number"), but the "Phone number" is in the "Clan Name" column.
REPLACE
destValues.push(rowValues[0][0]);// copy data from col A to col A
destValues.push(rowValues[0][2]);// copy data from col C to col B
destValues.push(rowValues[0][1]);// copy data from col B to col C
destValues.push(rowValues[0][3]);// copy data from col D to col D
var dest = SpreadsheetApp.getActive().getSheetByName('Updates');
dest.getRange(dest.getLastRow()+1,1,1,destValues.length).setValues([destValues]);
WITH
var date = new Date()
destValues.push(rowValues[0][0]);// copy data from col A to col A
destValues.push(rowValues[0][2]);// copy data from col C to col B
destValues.push(date);// copy data from col B to col C
var dest = SpreadsheetApp.getActive().getSheetByName('Updates');
dest.getRange(dest.getLastRow()+1,1,1,destValues.length).setValues([destValues]);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论