英文:
How Do I Copy Non-Adjacent Cell Values to a Row for a Log Sheet?
问题
我想要复制三个非相邻的单元格(AA9,AA12和AA15),然后粘贴它们到另一个工作表中的单行,就像一个日志表(A2,B2,C2)。我使用 "getLastRow+1" 来实现这个目标。目前,我只能使用 getValue 和 setValue 来处理单个单元格(AA9),在网上寻找类似示例后,我仍然无法让它起作用。
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Main");
var destSheet = ss.getSheetByName('Log');
// 从主工作表获取单元格的值
var streak = sheet.getRange('AA9').getValue();
// 在日志工作表上设置值
destSheet.getRange(destSheet.getLastRow()+1,1).setValue(streak);
我难以理解如何正确实现我想要的功能。我基本上想要复制AA9、AA12和AA15中的值,然后将它们粘贴(或使用setValue)到另一个工作表的A2、B2和C2中。
我正在慢慢学习并更好地理解所有这些,来自谷歌搜索的示例已经帮助了我很多,但这对我来说是一个难题。
- 我尝试使用命名区域,但不知道如何找到该命名区域的最后一个空行。
- 我尝试创建一个包含这些单元格的数组,但不知道如何正确使用setValues(出现有关行数不等于数据行数的错误,类似的错误)。
// 这没有起作用
var results = ['AA9','AA12','AA15'];
var streak = sheet.getRangeList(results).getRanges().map(range => range.getValues());
destSheet.getRange(destSheet.getLastRow()+1,1).setValues(streak);
- 我还搜索了谷歌很多,虽然搜索结果已经帮助我编写了一个随机数生成器的脚本(未在此处显示),但我仍然难以完成更基本的任务。
英文:
I want to copy three cells, which are non-adjacent (AA9, AA12, and AA15), and paste them on another sheet in a single row, like a log sheet. (A2,B2,C2) I'm using "getLastRow+1" to make this happen. Currently, I'm only able to use getValue and setValue for a single cell (AA9) and after searching for similar examples online, I'm still struggling to make this work.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Main")
var destSheet = ss.getSheetByName('Log');
// Get the Cell Values from Main Sheet
var streak = sheet.getRange('AA9').getValue();
// Set the Values on Log Sheet
destSheet.getRange(destSheet.getLastRow()+1,1).setValue(streak);
I'm struggling to understand how to correctly implement what I want. I essentially want to copy the values in AA9, AA12, and AA15, and paste them (or use setValue) in A2,B2, and C2 on the other sheet.
I'm slowly learning and getting a better understanding of everything, and examples from google searches have gotten me far, but this is a headscratcher for me.
- I've tried using Named Ranges, but I didn't understand how to find the last EMPTY row of that named range.
- I've tried creating an Array for those cells, but didn't understand how to use SetValues correctly (got an error about the number of rows not being equal to the data rows, something like that)
// THIS DIDN"T WORK
var results = ['AA9','AA12','AA15'];
var streak = sheet.getRangeList(results).getRanges().map(range => range.getValues());
destSheet.getRange(destSheet.getLastRow()+1,1).setValues(streak);
- I've also searched google quite a bit, and while the results have helped me script a random number generator (not shown here), I still struggle with more basic tasks.
答案1
得分: 1
你可以通过使用 Range.getValue()
而不是 Range.getValues()
来使你后面的代码示例工作。
要将值轻松附加到目标工作表,可以使用 Sheet.appendRow()
,如下所示:
const sheet = ss.getSheetByName('Main');
const destSheet = ss.getSheetByName('Log');
const data = sheet.getRangeList(['AA9', 'AA12', 'AA15'])
.getRanges()
.map(range => range.getValue());
destSheet.appendRow(data);
英文:
You can make your latter code example work by using Range.getValue()
instead of Range.getValues()
.
To easily append the values to the destination sheet, use Sheet.appendRow()
, like this:
const sheet = ss.getSheetByName('Main');
const destSheet = ss.getSheetByName('Log');
const data = sheet.getRangeList(['AA9', 'AA12', 'AA15'])
.getRanges()
.map(range => range.getValue());
destSheet.appendRow(data);
答案2
得分: 0
以下是您要的代码的中文翻译部分:
尝试这种方式:
function myfunk() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet0');
const dsh = ss.getSheetByName('Sheet1');
const [a,,,b,,,c] = sh.getRange("AA9:AA15").getValues().flat();
dsh.appendRow([a,b,c]);
}
英文:
Try it this way:
function myfunk() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet0');
const dsh = ss.getSheetByName('Sheet1');
const [a,,,b,,,c] = sh.getRange("AA9:AA15").getValues().flat();
dsh.appendRow([a,b,c]);
}
答案3
得分: 0
当我使用getValues时,它运行得很完美!但它确实抛出一个错误,说:“异常:参数(number[])与SpreadsheetApp.Range.setValues的方法签名不匹配。” 但再说一次,它实际上在我的电子表格上运行。
当我使用getValue时,奇怪的是它运行正常,不抛出错误,它适当地设置了一个追加行中的值,但然后又循环回来,反复在第一个单元格中设置值,尽管在下一行(因为它刚刚填满了上一行所以它下移了)。这太奇怪了。我可以看到它慢慢地这样做,它在行中设置值,过了一会儿,它又回到第一个值,但下移了一行,并再次设置该值。但其他的值都没有。
如果你能想象一下,它会发布3个数字,正确地追加在行中,然后在下一行中,它只会发布1个数字,而其他两个则为空白。
function log_data() {
const data = sheet.getRangeList(['AA9','AA12','AA15'])
.getRanges()
.map(range => range.getValue());
destSheet.appendRow(data);
destSheet.getRange(destSheet.getLastRow()+1,1).setValue(data);
}
英文:
When I use getValues, it works perfectly! But it does throw an error, saying "Exception: The parameters (number[]) don't match the method signature for SpreadsheetApp.Range.setValues.". But again, it does actually work on my spreadsheet.
When I use getValue, it strangely works, and doesn't throw an error, it sets the values appropriately in an appended row, but then loops back around again and repeatedly sets the value in the first cell again, albeit one row down (because it just filled the previous row so it moves down). It's so strange. I can see it do it slowly, it sets the values in the row, a split second later it loops back to the first value, but one row down, and sets that value again. But none of the other ones.
If you can visualize this, it'll post 3 numbers, correctly, appended in the row, then one row down it'll post only 1 number, but leave the other two blank.
function log_data() {
const data = sheet.getRangeList(['AA9','AA12','AA15'])
.getRanges()
.map(range => range.getValue());
destSheet.appendRow(data);
destSheet.getRange(destSheet.getLastRow()+1,1).setValue(data);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论