0

I'm completely new to scripts and need a script for Apps Script to reset cell(s) values to a value determined in another set of cells.

Say, when function Reset() is ran, L9 should change to the value in M9. This function should at the same time set value at L10 to M10, L11 to M11, and L12 to M12.

Any help would be greatly appreciated, and a bit of explaining what each line is doing would be great to help me learn!

I was running a function to decrease the L row values as follows:

function Fire1() {
   const sheet =  SpreadsheetApp.getActiveSheet();
   const range =  sheet.getRange("L9");

   range.setValue(range.getValue()-1);
 }

and tried different things like

function Reset() {
  const sheet =  SpreadsheetApp.getActiveSheet();
  const range =  sheet.getRange("L9");

 range.setValue(range.getValue("M9"));
}

again, barely understanding what's going on.

1 Answer 1

0

From Say, when function Reset() is ran, L9 should change to the value in M9. This function should at the same time set value at L10 to M10, L11 to M11, and L12 to M12., I believe your goal is as follows.

  • You want to copy a value of cell "M9" to the cells "L9:M12" using Google Apps Script.

In your showing script Reset(), I think that range.setValue(range.getValue("M9")); occurs an error. Because getValue has no argument.

In this case, how about the following sample script?

Sample script 1:

In this sample, copyTo is used.

function Reset() {
  const sheet = SpreadsheetApp.getActiveSheet();
  
  // Retrieve range "M9".
  const srcRange = sheet.getRange("M9");

  // Retrieve range "L9:M12".
  const dstRange = sheet.getRange("L9:M12");

  // Copy cell value "M9" to "L9:M12".
  srcRange.copyTo(dstRange, { contentsOnly: true });
}

When this script is run, the cell value of "M9" is copied to the cells "L9:M12".

Sample script 2:

In this sample, getValue and setValue are used.

function Reset() {
  const sheet = SpreadsheetApp.getActiveSheet();

  // Retrieve range "M9".
  const srcRange = sheet.getRange("M9");

  // Retrieve range "L9:M12".
  const dstRange = sheet.getRange("L9:M12");

  // Copy cell value "M9" to "L9:M12".
  dstRange.setValue(srcRange.getValue());
}

When this script is run, the cell value of "M9" is copied to the cells "L9:M12".

References:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.