简体   繁体   English

用 Excel 公式填充列

[英]Populating column with Excel formula

I'm fairly new to python.我对python相当陌生。

I have been struggling with getting an excel formula to actually populate the formula in my spreadsheet.我一直在努力寻找一个 excel 公式来实际填充我的电子表格中的公式。 What I'm trying to do is in column1 if it contains the word dog, the corresponding column populates with blue and if column one contains the word cat that column2 populates with red.我想要做的是在 column1 中,如果它包含单词 dog,则相应的列填充为蓝色,如果第一列包含单词 cat,则 column2 填充为红色。

Snippets from my code because it's stored elsewhere unless I need to rewrite it from top to bottom, just let me know.来自我的代码的片段,因为它存储在其他地方,除非我需要从上到下重写它,请告诉我。

I started off by creating a new column but I want to populate the row with a formula.我首先创建了一个新列,但我想用公式填充该行。

df.insert(2, 'Column2', "")

The actual Excel code I want to use =IF(ISNUMBER(SEARCH("dog", A2)), blue, IF(ISNUMBER(SEARCH("cat", H2)), red, "NOT FOUND")我想使用的实际 Excel 代码=IF(ISNUMBER(SEARCH("dog", A2)), blue, IF(ISNUMBER(SEARCH("cat", H2)), red, "NOT FOUND")

I've tried worksheet.write_formula('B2', 'IF(ISNUMBER(SEARCH("dog", A2)), blue, IF(ISNUMBER(SEARCH("cat", H2)), red, "NOT FOUND"))我试过worksheet.write_formula('B2', 'IF(ISNUMBER(SEARCH("dog", A2)), blue, IF(ISNUMBER(SEARCH("cat", H2)), red, "NOT FOUND"))

and df.append({"B2": 'IF(ISNUMBER(SEARCH("dog", A2)), blue, IF(ISNUMBER(SEARCH("cat", H2)), red, "NOT FOUND")}, ignore_index=True)df.append({"B2": 'IF(ISNUMBER(SEARCH("dog", A2)), blue, IF(ISNUMBER(SEARCH("cat", H2)), red, "NOT FOUND")}, ignore_index=True)

among others that I don't remember.其中包括我不记得的。 I'm also trying to populate the whole column.我也在尝试填充整个列。 Any insight would be very much appreciated.任何见解将不胜感激。 Everything I've tried so far either doesn't populate the cell or posts the code as a text.到目前为止,我尝试过的所有内容都不会填充单元格或将代码作为文本发布。

What is wrong with using Conditional Formatting?使用条件格式有什么问题?

条件格式

In the words of Tom Scott (AKA an absolute legend) "Why code things that already exist?"用 Tom Scott (又名绝对传奇)的话来说: “为什么要对已经存在的事物进行编码?”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM