[英]Unwanted multiple row entries / copies (Google Form --> Google Sheet file, sheet1 --> Google Sheet file, sheet 2)
我在 GSheet 中編寫了一個應用程序腳本來:
不幸的是,它多次將表單表的最后一行復制到相應的表中。 有時兩次,有時四次。 我在代碼中看不到我的錯誤。
到目前為止我找不到解決方案。 我在 appendRow 與 Utilities.sleep(5000) 之前包含了一個暫停,但沒有效果。
function myFunction() {
// Source sheet: Form is the source sheet of the Google Form
var source =
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form");
// Get last row in Form
var lastrow = source.getLastRow();
// Get just the date of the Form input
var formdate = source.getRange(lastrow,7,1,7).getValue();
// Change month number to string (e.g. April)
var currentD2 = Utilities.formatDate(formdate,
Session.getScriptTimeZone(), "MMMMM");
// Identify target sheet in same Google sheet file
var target =
SpreadsheetApp.getActiveSpreadsheet().getSheetByName(currentD2);
// Identify the appropriate range of the last row of the Form sheet
// (source)
var sourceData = source.getRange(lastrow, 2,1,8).getValues();
// Append the last row of the Form sheet to the last row in the target
// sheet
target.appendRow(sourceData[0])
}
我希望該函數只復制表單的最后一行一次,而不是多次。
您好我猜您正在使用onEdit觸發器來啟動您的功能。
這意味着當您更改“Google表格”時,它會觸發您的功能,該功能會在同一電子表格中更改另一張表格,從而再次啟動您的觸發器。
我建議將功能重命名為onEdit(e)ref( https://developers.google.com/apps-script/guides/triggers/events )
然后使用以下條件調整您的操作:如果e.range屬於您的“Google表單”,則執行其他操作。
function onEdit(e) {
if (e.range.getSheet().getName()='your google form sheet') {
// insert here the contents of your function
}
}
你可以使用這個:
function appendEntry(){
var ss = SpreadsheetApp.getActiveSpreadsheet()
var ws = ss.getSheetByName("data") // this is the sheet where new entry arrive
var ws2 = ss.getSheetByName("target") // this is the sheet where the entries will go
var data = ws.getDataRange().getValues()
var data2 = ws2.getDataRange().getValues()
for(var i=0;i<data.length;i++){
var row = data[i]
for(var k=0;k<data2.length;k++){
var row2 = data[k]
if(row2[0] != row[0]){
} // This will loop through both sheet
// If entry is already mentioned in other sheet the data will not append
}
}
ws2.appendRow(data[k]) //other wise entry will append to the next sheet
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.