当手动填充同一行中的另一排另一个单元格时,自动填充了Google的单元格

发布于 2025-02-08 08:59:55 字数 491 浏览 2 评论 0 原文

在Google表中,我想创建一个宏,该宏将在手动填充该行中的另一行时自动填充每一行的列。自动填充的单元格将使用一个公式,该公式采用了一部分信息(日期),该信息已手动输入并使用公式将其与随机数相连,以创建唯一的ID。插入和执行公式后,宏需要复制然后粘贴“值”该公式的结果。关键是要根据触发事件自动创建稳定的ID(行的日期输入)。

在伪代码中,这是我希望宏执行的过程:

when (date in yyyy-mm-dd format entered into A[i]) {

  fill B[i] with =CONCATENATE(SUBSTITUTE(LEFT(A[i], 7), "-", ""),RANDBETWEEN(0,1000000000));
  copy B[i];
  PASTE_VALUES B[i] in B[i];

}

如果我忽略了以前的答案来解决此问题,则表示歉意。我不是编码的新手,但是我是Google表中编码的新手,不确定要用来描述我所追求的条款或短语。

In Google Sheets, I want to create a macro that will automatically populate a column in each row when another column in that row is manually filled. The autofilled cell will use a formula that takes a chunk of the information (date) that's been entered manually and use a formula to concatenate it with a random number in order to create a unique ID. After inserting and executing the formula, the macro needs to copy and then paste "values only" the result of that formula. The point is to automatically create a stable ID in response to a triggering event (entry of date in row).

In pseudocode, here's the process I'd like the macro to execute:

when (date in yyyy-mm-dd format entered into A[i]) {

  fill B[i] with =CONCATENATE(SUBSTITUTE(LEFT(A[i], 7), "-", ""),RANDBETWEEN(0,1000000000));
  copy B[i];
  PASTE_VALUES B[i] in B[i];

}

Apologies if I've overlooked a previous answer that solves this problem. I'm not new to coding, but I am new to coding in Google Sheets and am not sure what terms or phrases to use to describe what I'm after.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别低头,皇冠会掉 2025-02-15 08:59:56

我相信您的目标如下。

  • 例如,当将 yyyy-mm-dd格式的值放在单元格“ a1”中时,您想将 = concatenate的公式放置7),“ - ”,“”),randbetbetewewnek(0,1000000000))到“ b1”。
  • 您想将公式的值固定为值。
  • 您想使用OnEdit触发器来实现此目标。
  • 添加:当列“ B”为空时,您想将值放在“ B”列中。

在这种情况下,以下示例脚本怎么样?

示例脚本:

请将以下脚本复制到电子表格的脚本编辑器,然后保存脚本。而且,请设置要使用的表名称。当您使用此脚本时,请将带有 yyyy-mm-dd的格式的值放在“ A”列中,因此,该脚本已运行。

function onEdit(e) {
  const sheetName = "Sheet1"; // Please set the sheet name.

  const range = e.range;
  const sheet = range.getSheet();
  const [a, b] = range.offset(0, 0, 1, 2).getDisplayValues()[0];
  if (sheet.getSheetName() != sheetName || range.columnStart != 1 || !/\d{4}-\d{2}-\d{2}/.test(a) || b) return;
  const dstRange = range.offset(0, 1);
  dstRange.setFormula(`=CONCATENATE(SUBSTITUTE(LEFT(${range.getA1Notation()}, 7), "-", ""),RANDBETWEEN(0,1000000000))`);
  SpreadsheetApp.flush();
  dstRange.copyTo(dstRange, { contentsOnly: true });
}

参考:

I believe your goal is as follows.

  • For example, when a value with the format of yyyy-mm-dd is put to the cell "A1", you want to put the formula of =CONCATENATE(SUBSTITUTE(LEFT(A1, 7), "-", ""),RANDBETWEEN(0,1000000000)) to the cell "B1".
  • You want to fix the value of the formula as the value.
  • You want to achieve this using OnEdit trigger.
  • Added: You want to put the value to the column "B", when the column "B" is empty.

In this case, how about the following sample script?

Sample script:

Please copy and paste the following script to the script editor of Spreadsheet, and save the script. And, please set the sheet name you want to use. When you use this script, please put the value with the format of yyyy-mm-dd to the column "A", by this, the script is run.

function onEdit(e) {
  const sheetName = "Sheet1"; // Please set the sheet name.

  const range = e.range;
  const sheet = range.getSheet();
  const [a, b] = range.offset(0, 0, 1, 2).getDisplayValues()[0];
  if (sheet.getSheetName() != sheetName || range.columnStart != 1 || !/\d{4}-\d{2}-\d{2}/.test(a) || b) return;
  const dstRange = range.offset(0, 1);
  dstRange.setFormula(`=CONCATENATE(SUBSTITUTE(LEFT(${range.getA1Notation()}, 7), "-", ""),RANDBETWEEN(0,1000000000))`);
  SpreadsheetApp.flush();
  dstRange.copyTo(dstRange, { contentsOnly: true });
}

Reference:

哆啦不做梦 2025-02-15 08:59:56

这是我想到的脚本:

/** @OnlyCurrentDoc */

function onEdit(e) { //Runs every time the sheet is edited
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Sheet1'); //Change this to whatever your sheet is named
  var inputCol = sheet.getRange('A1'); //Change this to whatever column the date is going to be entered

  //This is the range that will be checked. Slightly redundant, but makes it easier to reuse this script without needing to retype every variable
  var myRange = inputCol;

  //Get the row & column indexes of the active cell
  var row = e.range.getRow();
  var col = e.range.getColumn();

  //Check that your edited cell is within the correct column
  if (col == myRange.getColumn()) { //This runs only if a value is entered into the column defined in 'inputCol'
    if(sheet.getRange(e.range.getA1Notation()).getValue() == '') {return}; //If the edited cell is empty (ie the date is deleted, nothing happens)
    if(row == 1) {return} //If the header is changed, nothing happens
    let codeCell = sheet.getRange('B'+row); //Change to the column that will store the generated code value
    codeCell.setValue('=CONCATENATE(SUBSTITUTE(LEFT(A'+row+', 7), "-", ""),RANDBETWEEN(0,1000000000))');
    let hardValue = codeCell.getValue(); //Gets the value from the formula you just entered
    codeCell.setValue(hardValue); //Replaces the formula with just the resulting value
  };
}

包括评论以解释正在发生的一切。下面的链接是我用来测试的电子表格。它设置为允许编辑,因此请随时使用它自己测试脚本。

希望这会有所帮助!

This is the script I came up with:

/** @OnlyCurrentDoc */

function onEdit(e) { //Runs every time the sheet is edited
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Sheet1'); //Change this to whatever your sheet is named
  var inputCol = sheet.getRange('A1'); //Change this to whatever column the date is going to be entered

  //This is the range that will be checked. Slightly redundant, but makes it easier to reuse this script without needing to retype every variable
  var myRange = inputCol;

  //Get the row & column indexes of the active cell
  var row = e.range.getRow();
  var col = e.range.getColumn();

  //Check that your edited cell is within the correct column
  if (col == myRange.getColumn()) { //This runs only if a value is entered into the column defined in 'inputCol'
    if(sheet.getRange(e.range.getA1Notation()).getValue() == '') {return}; //If the edited cell is empty (ie the date is deleted, nothing happens)
    if(row == 1) {return} //If the header is changed, nothing happens
    let codeCell = sheet.getRange('B'+row); //Change to the column that will store the generated code value
    codeCell.setValue('=CONCATENATE(SUBSTITUTE(LEFT(A'+row+', 7), "-", ""),RANDBETWEEN(0,1000000000))');
    let hardValue = codeCell.getValue(); //Gets the value from the formula you just entered
    codeCell.setValue(hardValue); //Replaces the formula with just the resulting value
  };
}

Comments are included to explain everything that is happening. Linked below is the spreadsheet I used to test this. It is set to allow editing, so feel free to use it to test the script yourself.

https://docs.google.com/spreadsheets/d/1UONgRPBEbxn8CQeiRSPS4eFKHjh4ae8hXGYn6ImHxeI/edit?usp=sharing

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文