0
我有一個谷歌腳本我正在使用,我想插入我正在使用的當前行到一個值,這將重新添加到我在電子表格中設置的公式中。如果有人重新提交相同的數據,我想這樣做,所以他們通過電子郵件發送了更改。目前這個腳本只是我的一個概念證明,所以我可以繼續編寫整個過程。如何通過Google Apps腳本獲取Google Spread Sheet的行號?
我在腳本中遇到的問題是值currentRow。它似乎獲得第一遍的價值,然後不會改變它,當您有多個條目進行處理時,這是有問題的。
我在變量RESEND_EMAIL中使用值currentRow,因此它可以生成一個公式,它可以查看兩列是否相似(我將使用日期時間戳記並將其在第一次複製到列D(也稱爲var resend [4])
這樣我就可以將腳本設置爲自動運行,並檢查此列是否設置爲「是」,在這種情況下,它將再次發送併發送電子郵件,然後更改一個值(這裏顯示爲電子郵件地址 - 但我將使用時間戳來代替),以便公式RESEND_EMAIL更改重新發回爲「否」。
如何始終確保currentRow值的正確行數?
// This constant is written in column C for rows for which an email
// has been sent successfully.
var EMAIL_SENT = "EMAIL_SENT";
function sendEmails2() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 2; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, 6)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[0]; // First column
var message = row[1]; // Second column
var emailSent = row[2]; // Third column
var resend = row[4];
var extension = row [5];
var currentRow = sheet.getActiveRange().getRow();
var RESEND_EMAIL = "=IF(A"+ currentRow +"=D" + currentRow +",\"No\",\"Yes\")";
if (emailSent != EMAIL_SENT) { // Prevents sending duplicates
var subject = "Sending emails from a Spreadsheet";
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(startRow + i, 3).setValue(EMAIL_SENT);
sheet.getRange(startRow + i, 5).setFormula(RESEND_EMAIL);
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}
if (resend == "Yes") { // Prevents sending duplicates
var subject = "Sending emails from a Spreadsheet";
message = message + " " + extension;
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(startRow + i, 4).setValue(emailAddress);
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}
}
}
這不是關於您的答案的評論,而是關於改進問題。你一直在回答很多GAS問題!當你這樣做時,如果你發現問題需要改進,包括重新簽名,請花一兩分鐘來修改它。獎勵:當這個問題得到[google-apps-script]的正確標記時,您將在[最高用戶列表](http://stackoverflow.com/tags/google-apps-script/topusers)中獲得認可, ! (我在過去幾個月裏已經重複了幾百個問題,並且可以使用一些幫助!) – Mogsdad
@Mogsdad完美,我會的。非常感謝你。 – wchiquito